The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker‘s personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:
Itai nyan~ (It hurts, nyan~)
Ninjin wa iyada nyan~ (I hate carrots, nyan~)
Now given a few lines spoken by the same character, can you find her Kuchiguse?
Input Specification:
Each input file contains one test case. For each case, the first line is an integer N (2≤N≤100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character‘s spoken line. The spoken lines are case sensitive.
Output Specification:
For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.
Sample Input 1:
3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~
Sample Output 1:
nyan~
Sample Input 2:
3
Itai!
Ninjinnwaiyada T_T
T_T
Sample Output 2:
nai
1 /**************************************************** 2 PAT A1077 Kuchiguse 3 ****************************************************/ 4 #include <iostream> 5 #include <vector> 6 #include <string> 7 8 using namespace std; 9 10 int main() { 11 int sentenceCnt = 0; 12 vector<string> Sentences; 13 14 cin >> sentenceCnt; 15 16 string temp; 17 getchar(); //接收换行符 18 int minSenLen = 300; //记录最短的句子的长度,句子最长为256因此初始值设置为300 19 for (int i = 0; i < sentenceCnt; ++i) { 20 getline(cin, temp); 21 Sentences.push_back(temp); 22 23 minSenLen = minSenLen < temp.size() ? minSenLen : temp.size(); 24 } 25 26 string ans; //ans长度小于最长度最短的那句话 27 for (int i = 0; i < minSenLen; ++i) { 28 char c = Sentences[0][Sentences[0].size() - 1 - i]; 29 for (int j = 0; j < sentenceCnt; ++j) { 30 if (c != Sentences[j][Sentences[j].size() - 1 - i]) { //出现了不同句子到数相同的位置的字符不相同的情况 31 cout << (ans.size() == 0 ? "nai" : ans); 32 return 0; 33 } 34 } 35 ans.insert(ans.begin(), c); 36 } 37 38 cout << ans; //如果ans等于长度最小的那句话 39 40 return 0; 41 }
1:注意最后寻找“Kuchiguse”的时候如果它的长度小于所有句子的长度,那么上述代码中最后一个for循环就可以处理了。但是如果“Kuchiguse”的长度等于所最短句子的长度for循环是不会处理的因此在循环之后还要添加输出ans的语句,这也是题目中最后一个测试点测试的地方。
原文:https://www.cnblogs.com/mrdragon/p/11414396.html