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
题意:找最大相同的后缀
思路:用vector容器储存字符串,两个循环搞定,外循环,所有字符串最小长度,内循环,每个字符串的最后一位字符是否相等,维护一个string来保存相等的字符,只要有一个不相等,直接跳出两层循环,然后判断string是否为空,若为空,则说明没有相同的后缀,反之,将string逆序输出即可~
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<string> vec(n);
char c;
c = getchar();
int min_len = 0x7fffffff;
for(int i = 0; i < n; i++){
getline(cin, vec[i]);
min_len = min(min_len, int(vec[i].length()));
}
string s = "";
char temp;
bool flag = true;
for(int j = 1; j <= min_len; j++){
for(int i = 0; i < n - 1; i++){
if(vec[i][vec[i].length() - j] != vec[i + 1][vec[i + 1].length() - j]){
flag = false;
break;
}
temp = vec[i][vec[i].length() - j];
}
if(flag){
s += temp;
}else
{
break;
}
}
reverse(s.begin(), s.end());
if(s == ""){
cout << "nai" << endl;
}else
{
cout << s << endl;
}
return 0;
}
本文探讨了日语中独特的句子结尾粒子,这些粒子反映了说话者的个性。通过分析动漫和漫画中角色的语言习惯,即“Kuchiguse”,文章提供了一个算法,用于识别并找出角色对话中的共同后缀,以此来确定其语言偏好。
187

被折叠的 条评论
为什么被折叠?



