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
1 #include<stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 #include<string.h> 5 6 int main() 7 { 8 int n, i, j, k = 0; 9 char str[110][300]; 10 scanf("%d", &n); 11 getchar(); 12 int len, minlen = 300; 13 for(i = 0; i < n; i++) 14 { 15 gets(str[i]); 16 len = strlen(str[i]); 17 if(len < minlen) 18 { 19 minlen = len; 20 } 21 for(j = 0; j < len / 2; j++) 22 { 23 char temp = str[i][j]; 24 str[i][j] = str[i][len - 1 - j]; 25 str[i][len - 1 - j] = temp; 26 } 27 } 28 for(j = 0; j < minlen; j++) 29 { 30 int flag = 1; 31 char temp = str[0][j]; 32 for(i = 0; i < n; i++) 33 { 34 if(str[i][j] != temp) 35 { 36 flag = 0; 37 break; 38 } 39 } 40 if(flag) 41 k++; 42 else 43 break; 44 } 45 if(k <= 0) 46 printf("nai "); 47 else 48 { 49 for(j = k - 1; j >= 0; j--) 50 { 51 printf("%c", str[0][j]); 52 } 53 printf(" "); 54 } 55 return 0; 56 57 }