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?
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
/**
本题题意:
给定n个字符串, 输出n个字符串后缀相同的的所有字符
本题思路:
比较后缀, 只需要比较几个字符串最后的字符, 但是需要注意的问题是:
1.防止字符串越界, 取最短长度的字符串,
在输入时, 求出最短字符串的长度, 并且需要将字符串倒置
(每个字符串的长度不同, 最短长度的最后一个字符不是最长字符串的最后一个字符
因此需要在输入字符串时, 交换字符的顺序 )
此时:使用string数组ss, string数组 是可以 用ss[i][j]表示
2.还需要注意的一个问题是
求出最小长度时 需要求出min( mins, (int)ss.size()) ss数组的长度需要强转
3.对于读取一行字符串, c++中string是 getline(cin, 字符串变量名)
对于char二维数组为 gets() (c++ 11已经弃用这个方法)(新版pat也不支持使用)
**/
二维 char数组解决思路:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
char s[100][256];
int n, mins = 258, res = 0;
int main(){
scanf("%d", &n);
getchar();
for(int i = 0; i < n; i++){
getline(cin, s);
int len = strlen(s[i]); //
if(len < mins)
mins = len;
for(int j = 0; j < len / 2; j++){
swap(s[i][j], s[i][len - j - 1]);
}
}
for(int i = 0; i < mins; i++){
if(res != 0)
break;
char temp = s[0][i];
for(int j = 1; j < n; j++){
if(s[j][i] != temp){ //这样做存在一个问题 每个字符串的长度不同, 最短长度的最后一个字符不是最长字符串的最后一个字符
//因此需要在输入字符串时, 交换字符的顺序
res = i - 1;
break;
}
}
}
cout << res << endl;
if(res == -1)
printf("nai\n");
else{
for(int i = res; i >= 0; i--){
printf("%c", s[0][i]);
}
}
return 0;
}
string数组解决思路:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n, mins = 258;
string ss[100], res = "";
bool flag = false;
scanf("%d", &n);
getchar();
for(int i = 0; i < n; i++){
getline(cin, ss[i]);
mins = min((int)ss[i].size(), mins);
reverse(ss[i].begin(), ss[i].end());
}
for(int i = 0; i < mins && !flag; i++){
for(int j = 0; j < n - 1 && !flag; j++){
if(ss[j][i] != ss[j + 1][i])
flag = true;
}
if(!flag)
res += ss[0][i];
}
reverse(res.begin(), res.end());
printf("%s\n", res == "" ? "nai" : res.c_str());
return 0;
}
参考柳神在输入时处理字符串解决思路:
/**
柳神的思路是从输入的时候开始处理
**/
#include<iostream>
#include<algorithm>
using namespace std;
int n, mins = 258;
string s, res;
int main(){
scanf("%d", &n);
getchar();
for(int i = 0; i < n; i++){
getline(cin , s);
reverse(s.begin(), s.end());
if(i == 0){ //0这种特殊情况作特殊处理, 因为此时res 为 ""就只有字符串""与它有相同后缀
//如果没有字符串"", 显然其他字符串不能匹配
res = s;
continue;
}else{
if(res.size() > s.size()) //此步交换 , 可以确保最长长度相同(长度 = 此时最小的字符串)的情况
//结果字符串依然成立
swap(res, s);
for(int j = 0; j < min(s.size(), res.size()); j++)
if(s[j] != res[j]){
res = res.substr(0, j);
break;//判断完毕 , 判断下一个数
}
}
}
reverse(res.begin(), res.end());
printf("%s", res.size() == 0 ? "nai" : res.c_str());
return 0;
}