题目要求:
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
解题思路: 由于判断共同后缀,则将字符串反转再判断可以省很多事。反转后,判断第0位是否为共同后缀(必须所有的字符串的这一位都要相同,可以以第一个字符串作为基准,判断其他字符串的第0位是否与其相同 ),然后再判断第一位,第二位。。。。。。只要有一位不是所有字符串都相同,则判断结束
参考代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{int minLen = 256; //假设一个最大值(随意定)。int N;cin >>N;int ans = 0;char s[102][256]; //使用二维数组来存储字符串getchar(); //因为随后要输入的是字符类型,所以要使用getchar先将换行符取走for(int i=0;i<N;i++){gets(s[i]);//寻找最短的那个字符串的长度,因为共同后缀只需要以最短的字符串的长度为基准int len = strlen(s[i]);if(len<minLen)minLen = len;//将数组进行反转,因为反转后容易代码实现for(int j = 0;j<strlen(s[i])/2;j++){char b;b = s[i][j];s[i][j] = s[i][strlen(s[i])-1-j];s[i][strlen(s[i])-1-j] = b;}}for(int i=0;i<minLen;i++){int flag = 1; //flag为1表明此字符为共同后缀,此代码先假设为共同后缀char a = s[0][i]; //将第一个字符串的第i位赋值给a//将a与每个字符串的第i位比较,如有一个不相等,则表明共同后缀不包括此字符,即结束判断共同后缀for(int j=0;j<N;j++) {if(a != s[j][i]){flag = 0; //因为代码开始假设有共同后缀,所以此时要设置标志位为0表明标志位到此处结束break;}}//当flag为1即未被置为0时,则使ans+1表示增加一位共同后缀,并开始判断下一位//当flag为0时表明某一位不是共同后缀,则判断到此为止if(flag)ans++;elsebreak;}if(ans == 0)cout << "nai";elsefor(int i=ans-1;i>=0;i--)cout <<s[0][i];return 0;
}
此外,本人还写出了一种实现方法,但有诸多测试点错误,在此贴出,望各位大神能看到后给予宝贵的解答
#include <iostream>
#include <cstring>
using namespace std;
//反转字符串
void reverse_fun(string *a)
{for(int i = 0;i<(*a).length()/2;i++){char b;b = (*a)[i];(*a)[i] = (*a)[(*a).length()-1-i];(*a)[(*a).length()-1-i] = b;}
}int main()
{int minLen = 259;int N;cin >>N;int flag = 0; //flag为0表明没有共同后缀char common[100];string s[102]; //使用字符串数组来存储输入getchar(); //获取换行符for(int i=0;i<N;i++){getline(cin,s[i]);reverse_fun(&s[i]);//统计最短字符串长度int len = s[i].length();if(len<minLen)minLen = len;}for(int i=0;i<minLen;i++){int true1 = 0;char a = s[0][i]; //将第一个字符串的第i位赋值给afor(int j=0;j<N;j++) //将a与每个字符串的第i位比较{if(a == s[j][i]){true1++; //a与一个字符串的此位相同则true1+1,如true1最终和N相同,则表明所有的字符串的此位都相同}elsebreak;}if(true1 == N){common[i] = a;flag = 1; //如果N个字符串的此位都相同,则表明此位为共同后缀,则flag置为1}}if(flag == 0)cout << "nai";elsefor(int i=strlen(common)-1;i>=0;i--)cout <<common[i];return 0;
}