PAT_A1077#Kuchiguse
Source:
Description:
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
Keys:
- 模拟题
Attention:
- s.insert(s.begin()+pos, str[i]),s.insert(pos, str)
- 使用getline(cin,str),注意吃掉上一行的换行符
Code:
1 #include<cstdio> 2 #include<string> 3 #include<iostream> 4 using namespace std; 5 6 int main() 7 { 8 #ifdef ONLINE_JUDGE 9 #else 10 freopen("Test.txt", "r", stdin); 11 #endif // ONLINE_JUDGE 12 13 int n; 14 string s,t; 15 scanf("%d\n", &n); 16 getline(cin,s); 17 for(int i=1; i<n; i++) 18 { 19 getline(cin,t); 20 string p; 21 int ps=s.size()-1,pt=t.size()-1; 22 while(ps>=0 && pt>=0 && s[ps]==t[pt]) 23 { 24 p.insert(p.begin(),s[ps]); 25 ps--; 26 pt--; 27 } 28 s = p; 29 } 30 if(s.size()) 31 cout << s; 32 else 33 printf("nai"); 34 35 return 0; 36 }