标签:
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:
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 #include<cstdio> 2 #include<cstring> 3 4 char sentence[110][270]; 5 int pt[110]; 6 int main(int argc, char *argv[]) 7 { 8 int n; 9 scanf("%d",&n); 10 getchar(); 11 for(int i=0;i<n;i++) 12 { 13 gets(sentence[i]); 14 pt[i]=strlen(sentence[i])-1; 15 } 16 int ans=-1; 17 bool flag=true; 18 for(int i=0;true;i++) 19 { 20 21 if(pt[0]-i<0) 22 { 23 flag=false; 24 break; 25 } 26 char ch=sentence[0][pt[0]-i]; 27 for(int j=0;j<n;j++) 28 { 29 if(pt[j]-i<0) 30 { 31 printf("Good\n"); 32 flag=false; 33 break; 34 } 35 if(sentence[j][pt[j]-i]!=ch) 36 { 37 flag=false; 38 break; 39 } 40 } 41 if(!flag) 42 break; 43 else 44 ans=pt[0]-i; 45 } 46 //printf("ans=%d",ans); 47 if(ans==-1) 48 printf("nai\n"); 49 else 50 { 51 for(int i=ans;sentence[0][i]!=‘\0‘;i++) 52 { 53 printf("%c",sentence[0][i]); 54 } 55 putchar(‘\n‘); 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4273040.html