1077. Kuchiguse (20)
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_TSample Output 2:
nai
题目其实也是一知半解,但就是AC了。。
1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 char line[105][300]; 8 bool cmp(char *a,char *b){ 9 return strlen(a)<strlen(b); 10 } 11 int main(){ 12 //freopen("D:\INPUT.txt","r",stdin); 13 int n,i; 14 scanf("%d",&n); 15 getchar(); 16 for(i=0;i<n;i++){ 17 cin.getline(line[i],300); 18 } 19 /*for(i=0;i<n;i++){ 20 cout<<line[i]<<endl; 21 }*/ 22 //sort(line,line+n,cmp); 23 int j=1,k,len; 24 char c; 25 string s=""; 26 for(i=1;i<=strlen(line[0]);i++){ 27 c=line[0][strlen(line[0])-i]; 28 for(k=1;k<n;k++){ 29 len=strlen(line[k]); 30 if(len<i||line[k][len-i]!=c){ 31 break; 32 } 33 } 34 if(k==n){ 35 s=c+s; 36 } 37 else{ 38 break; 39 } 40 } 41 if(s!=""){ 42 cout<<s<<endl; 43 } 44 else{ 45 cout<<"nai "; 46 } 47 }