本文主要是介绍【PAT】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:
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?
翻译:日语的句子末尾语气词是很臭名昭著的。对于语气词的个人偏爱可以被认为是说话者个人特质的一种映射。这样的偏爱叫做”Kuchiguse”,并且经常在动漫和漫画上进行艺术夸张。举个例子,人造句末尾语气词”nyan~”经常被用来形容一个像猫一样的人物。
INPUT FORMAT
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.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括一个正整数N(2<=N<=100)。跟着N句0-256个字符(包括)的句子,每个都表现了一个人物的口语。口语是大小写敏感的。
OUTPUT FORMAT
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”.
翻译:对于每组输入数据,输出一行一个人物的“kuchiguse ”。i.e., 即N行的最长公共后缀。如果没有这样的后缀,输出”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
解题思路
这道题就是找到所有字符串的最长后缀,可以倒序搜索每个字符串,直到某一个字符不相同为止,长度为最短字符串的长度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
char s[100][260];
int length[260];
string ans;
int main(){scanf("%d\n",&N);for(int i=0;i<N;i++)gets(s[i]),length[i]=strlen(s[i]);int flag=0;char temp;for(int j=0;;j++){if(flag==1)break;for(int i=0;i<N;i++){if(length[i]-1-j<0){flag=1;break;}if(i==0)temp=s[i][length[i]-1-j];else if(s[i][length[i]-1-j]!=temp){flag=1;break;}}if(!flag)ans.push_back(temp);}if(ans.length()>0)for(int i=ans.length()-1;i>=0;i--){printf("%c",ans[i]);}else printf("nai");printf("\n");return 0;
}
这篇关于【PAT】1077. Kuchiguse (20)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!