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:
1 2 3 4 |
3 Itai nyan~ Ninjin wa iyadanyan~ uhhh nyan~ |
Sample Output 1:
1 |
nyan~ |
Sample Input 2:
1 2 3 4 |
3 Itai! Ninjinnwaiyada T_T T_T |
Sample Output 2:
1 |
nai |
这道题好萌……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include<stdio.h> #include<stdlib.h> #include<string.h> /*#define ToLow(X) (((X)>='A'&&(X)<='Z')?(X)-'A'+'a':(X))*/ int main() { int i,j,n,suffix=1,flag; char **character,temp; scanf("%d",&n); getchar(); if(n==1) { printf("nai\n"); exit(0); } character=(char **)malloc(n*sizeof(char *)); for(i=0;i<n;i++) { character[i]=(char *)malloc(257*sizeof(char)); gets(character[i]); /* for(j=0;j<strlen(character[i]);j++) { ToLow(character[i][j]); }*/ } while(1) { flag=0; for(i=0;i<n;i++) { if(suffix>strlen(character[i])) { goto end; } if(flag==0) { flag=1; temp=character[i][strlen(character[i])-suffix]; } if(temp!=character[i][strlen(character[i])-suffix]) { goto end; } } suffix++; } end: /* while(character[0][strlen(character[0])-suffix]==' ') { suffix--; }*/ if(suffix==1) { printf("nai\n"); } else { suffix--; printf("%s\n",character[0]+strlen(character[0])-suffix); } } |