本文共 2238 字,大约阅读时间需要 7 分钟。
题目要求:
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#include #include 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 =0;i--) cout <
此外,本人还写出了一种实现方法,但有诸多测试点错误,在此贴出,望各位大神能看到后给予宝贵的解答
#include#include 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 =0;i--) cout <
转载地址:http://hbzfi.baihongyu.com/