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:
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
代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n;
lp: while(scanf("%d",&n)==1)
{
string *s=new string[n+1];
int minlen=257;
getchar();
for(int i=0;i<n;i++)
{
getline(cin,s[i]);
if(s[i].length()<minlen) minlen=s[i].length();
}
//重开数组
string *t=new string[n+1];
for(int i=0;i<n;i++)
{
for(int j=s[i].length()-1;j>=0;j--)
{
t[i].push_back(s[i][j]);
}
}
string res;
if(minlen==0)
{
printf("nai
");
goto lp;
}
if(minlen==1)
{
for(int j=1;j<n;j++)
{
if(t[j][0]!=t[0][0])
{
printf("nai
");
goto lp;
}
}
printf("%c
",t[0][0]);
goto lp;
}
for(int i=1;i<minlen;i++)
{
string hhd=t[0].substr(0,i);
int flag=1;
for(int j=1;j<n;j++)
{
if(t[j].substr(0,i)!=hhd)
{
if(i==1)
{
printf("nai
");
goto lp;
}
flag=0;
break;
}
}
if(flag==1)
{
res=hhd;
}
else
{
res=t[0].substr(0,i-1);
break;
}
}
for(int i=res.length()-1;i>=0;i--)
cout<<res[i];
printf("
");
goto lp;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。