forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoetry.cpp
78 lines (69 loc) · 2.47 KB
/
Poetry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <sstream>
#include <cstring>
#include <vector>
#include <cctype>
#include <map>
using namespace std;
class Poetry {
public:
string rhymeScheme(vector <string>);
string pattern(string s);
bool isvowel(char x, int p, int n) {
if(p == 0 || p == n-1)
return (x) == 'a' || (x) == 'e' || (x) == 'i' || (x) == 'o' || (x) == 'u';
return (x) == 'a' || (x) == 'e' || (x) == 'i' || (x) == 'o' || (x) == 'u' || (x) == 'y';
}
};
string Poetry::pattern(string s){
stringstream ss(s);
string t = "", temp = "";
while(ss>>temp) {
// cout<<temp<<endl;
t = temp;
}
for(int i = 0; i < t.size(); i++)
if(isupper(t[i])) t[i] = t[i]-'A' + 'a';
int n = t.size(); n--;
while(n >= 0 &&!isvowel(t[n], n, t.size())) n--;
while(n >= 0 && isvowel(t[n], n, t.size())) n--;
return t.substr(n+1, t.size()-n-1);
}
string Poetry::rhymeScheme(vector <string> poem) {
vector<char> ls(poem.size());
map<string, char> ms;
int c = 'a';
for(int i = 0; i < poem.size(); i++){
ls[i] = ' ';
string s = pattern(poem[i]);
// cout<<s;
if(s.empty()) ls[i] = ' ';
else{
if(ms.count(s) == 0) ms[s] = char(c++);
ls[i] = ms[s];
}
if(c == 'z' + 1) c = 'A';
}
string s = "";
for(int i = 0; i < ls.size(); i++){
s.push_back(ls[i]);
}
return s;
}
int main(){
string sl[] =
{" ",
"Measure your height",
"AND WEIGHT ",
"said the doctor",
"",
"And make sure to take your pills",
" to cure your ills",
"Every",
"DAY"}
//{" aioimoxabufo ", " diilbblmrsfeeucqwikiiazhxeorpxgsewwheuuanrsgityy", " azaiiaerefevpijiznha", " hwxrtbueauakegbetuejsuaueeukv ", "eafearpeaieoudaaojaeeloukoonitwweadviyiucjvivyidvn", "", " ueoophcmiuwqfuuoaptueejrcigpahujoaadoeflhudzjaom", "", "", "", " ", "luiaomzeatmadikduabaeeaehunxgiuoaaes", "", " ", " amdboihaelhaeiuikejfeoauboosemooy yuhwiah", "", " auainpwemijcv", "uaioiufretouqghlactlpssqbeakb ", "ttxukafxuuraquzarlubkkdonvioxeesaguhxmmumaeaeizaia", "avoeuogbqogciklhaavrcngstuielidelacwue ", " yeelouhbxowtaoeahsjrihxftfttkvcrxurojacrurouryyp", " ", " ", "", " ", " ", " vuuvmosxiya", " ", "oiiczezkerkeneeiiefoooxxxtljidluqpilaohhabenay ", "", ""}
;
vector<string> vs(sl, sl + sizeof(sl)/sizeof(sl[0]));
cout<<"ans = \n"<<Poetry().rhymeScheme(vs);
// cout<<Poetry().pattern("is a whole better than this");
}