PAT甲级——A1077 Kuchiguse
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). 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 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 int N; 7 int main() 8 { 9 cin >> N; 10 string res = "", str; 11 vector<string>v; 12 int minL = 300; 13 getchar(); 14 for (int i = 0; i < N; ++i) 15 { 16 getline(cin, str); 17 reverse(str.begin(), str.end()); 18 v.push_back(str); 19 minL = minL < str.length() ? minL : str.length(); 20 } 21 bool flag = true; 22 for (int i = 0; i < minL && flag; ++i) 23 { 24 for (int j = 1; j < N && flag; ++j) 25 { 26 if (v[0][i] != v[j][i]) 27 flag = false; 28 } 29 if (flag) 30 res += v[0][i]; 31 } 32 reverse(res.begin(), res.end()); 33 if (res.length() == 0) 34 cout << "nai" << endl; 35 else 36 cout << res << endl; 37 38 return 0; 39 }