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
/*
PS:PAT中是禁用了gets的
题意:找最长公共后缀
思路:
因为要的是 公共后缀 所以只要出现一个字符串的后缀与其他不同 则直接输出 nai即可
我们不妨将它转化为 求最长公共前缀的问题 这样使问题更加简单化
我么先反转字符串
然后找到长度最小的字符串 并且用len保留它的长度
此时最长公共前缀 最长也就是 这个字符串 即我们只需在区间[0, len)中找前缀即可
*/
getline也可以作为成员函数使用:
cin.getline(char *cha,int num,char f);
向cha中输入num个字符,输入过程中达到num-1个数或者提前遇到f字符,输入结束。
#include <iostream>
using namespace std;
#include <cstring>
char s[101][260];
int main(){
int n;
int minlen = 260;
cin >> n;
getchar(); //读取换行符
for(int i=0; i<n; i++){
cin.getline(s[i], 260); //向s[i]中输入字符串 默认以换行结束
int len = strlen(s[i]);// 获得当前字符串长度
if(len < minlen) minlen = len; //最小字符串长度
for(int j=0; j<len / 2; j++){ //反转字符串
char temp = s[i][len - 1 - j];
s[i][len - 1 - j] = s[i][j];
s[i][j] = temp;
}
}
int cnt = 0;
for(int i=0; i<minlen; i++){ //判断所有字符串的第i个字符是否相同
char c = s[0][i]; //取第一个字符串的第i个字符
bool same = true;
for(int j=1; j<n; j++){ //判断其余字符串的第i个字符是否等于c
if(s[j][i] != c){ //只要有一个不等,就停止枚举,说明公共前缀到此为止
same = false;
break;
}
}
if(same) cnt++; //若所有字符串的第i为相等 计数器cnt加一
else break;
}
if(cnt){
for(int i=cnt - 1; i >= 0; i--){
cout << s[0][i];
}
}else{
cout << "nai";
}
return 0;
}
fgets(char *a,number,iostream)
第一个参数是数组的首地址,第二个参数是数组中字符的最大长度,
第三个参数是输入流(玩具程序支配用stdin)。
fgets()注意点:fgets是会读入换行符的,读入换行符后,
fgets会把换行符存储为最后一个字符,并在后面加入一个’\0’作为结束符,
所以你输入的字符存到数组中会多出一个换行符,
当然如果你输入的字符数刚好等于第二个参数number-1,那么就不会有换行符存入了。
#include <iostream>
using namespace std;
#include <cstring>
char s[101][260];
int main(){
int n;
int minlen = 260;
cin >> n;
getchar(); //读取换行符
for(int i=0; i<n; i++){
fgets(s[i], 260, stdin);
//处理fgets最后的换行符
int len = strlen(s[i]);
s[i][len - 1] ='\0';
len--;
if(len < minlen) minlen = len; //最小字符串长度
for(int j=0; j<len / 2; j++){ //反转字符串
char temp = s[i][len - 1 - j];
s[i][len - 1 - j] = s[i][j];
s[i][j] = temp;
}
}
int cnt = 0;
for(int i=0; i<minlen; i++){ //判断所有字符串的第i个字符是否相同
char c = s[0][i]; //取第一个字符串的第i个字符
bool same = true;
for(int j=1; j<n; j++){ //判断其余字符串的第i个字符是否等于c
if(s[j][i] != c){ //只要有一个不等,就停止枚举,说明公共前缀到此为止
same = false;
break;
}
}
if(same) cnt++; //若所有字符串的第i为相等 cnt加一
else break;
}
if(cnt){
for(int i=cnt - 1; i >= 0; i--){
cout << s[0][i];
}
}else{
cout << "nai";
}
return 0;
}
再贴一个柳神的
分析:因为是后缀,反过来比较太麻烦,所以每输入一个字符串,就把它逆序过来再比较,就会比较容易啦~
首先ans = s;后来每输入的一个字符串,都和ans比较,如果后面不相同的就把它截取掉~最后输出ans即可(要逆序输出~所以先将ans倒置reverse一下~)
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
scanf("%d\n", &n);
//柳神一如既往的细腻,这里直接在scanf里面把换行符给读取了
string ans;
for(int i = 0; i < n; i++) {
string s;
getline(cin, s);
int lens = s.length();
reverse(s.begin(), s.end());
if(i == 0) {
ans = s;
continue;
} else {
int lenans = ans.length();
if(lens < lenans) swap(ans, s);
int minlen = min(lens, lenans);
for(int j = 0; j < minlen; j++) {
if(ans[j] != s[j]) {
ans = ans.substr(0, j);
break;
}
}
}
}
reverse(ans.begin(), ans.end());
if (ans.length() == 0) ans = "nai";
cout << ans;
return 0;
}