pat1077. Kuchiguse (20)

 

1077. Kuchiguse (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

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_T
Sample 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\n";
46   }
47 }

 

posted @ 2015-09-09 08:52  Deribs4  阅读(283)  评论(0编辑  收藏  举报