<[SRM 172][Div2][Div2 Level-1][Simple Search][... | [SRM 173][Div2][Div2 Level-1][Simple Math][St...>
2009-08-20
Poetry
SRM 170, Div2, Div2 Level-3, String Manipulation, 40% |
与えられた詩が、どのように音韻を踏んでいるのかを調べる。
377.34/1000
class Poetry { public: string rhymeScheme(vector <string> poem) { char label = 'a'; vector<vector<string> > substrings(poem.size()); string result(poem.size(), ' '); for (int i = 0; i < poem.size(); i++) { if (poem[i].length() == 0) continue; string s = tolowerString(poem[i]); istringstream iss(s); string last(""), tmp; while (iss >> tmp) last = tmp; if (last == "") continue; const int len = last.length(); bool isPreVowel = false; for (int j = len-1; j >= 0; j--) { const char c = last[j]; bool isVowel = (j!=len-1 && j!=0 && c=='y') || c=='a' || c=='e' || c=='i' || c=='o' || c=='u'; if (isPreVowel && !isVowel) substrings[i].push_back(last.substr(j+1)); isPreVowel = isVowel; } if (isPreVowel) substrings[i].push_back(last); for (int j = 0; j <= substrings[i].size(); j++) { if (j == substrings[i].size()) { result[i] = label; label++; if (label > 'z') label = 'A'; break; } for (int k = 0; k < i; k++) { if (find(substrings[k].begin(), substrings[k].end(), substrings[i][j]) != substrings[k].end()) { result[i] = result[k]; break; } } if (result[i] != ' ') break; } } return result; } private: string tolowerString(string s) { for (int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s; } };
コメントを書く