其他分享
首页 > 其他分享> > 1077 Kuchiguse (20 分)字符串共同后缀

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

解题思路

  题目大意: 日语当中,每句话的末尾通常户带有小品词(particles),这种词通常能反映一个人的性格,给出N句话,找到这N句话的共同后缀;
  解题思路: 因为字符串是长短不一的,如果从前往后找共同后缀,肯定会很麻烦,不如把字符串翻转过来,然后直接用两个for循环,逐字符的进行遍历即可,如果有相同的,则记录,如果不同,中断遍历,检查记录的单词个数,如果一个都没有,显然是没有共同后缀的,输出nai,如果有,则再做一次翻转,输出。

/*
** @Brief:No.1076 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2019-01-23
** @Solution: Accepted!
*/
#include<iostream>
#include<algorithm>
using namespace std;

string input[265];

int main(){
	int N;
	while(cin>>N){
		cin.get();
		for(int i=0;i<N;i++){
			getline(cin,input[i]);
			reverse(input[i].begin(),input[i].end());
		}
		string suffix;
		for(int i=0;i<input[0].size();i++){
			char c = input[0][i];
			bool isequal = true;
			for(int j=1;j<N;j++){
				if(input[j][i]!=c){
					isequal = false;
					break;
				}
			}
			if(isequal) suffix+=c;
			else break;
		}
		if(suffix.empty()) cout<<"nai"<<endl;
		else{
			reverse(suffix.begin(),suffix.end());
			cout<<suffix<<endl;
		}
	}
	return 0;
}

在这里插入图片描述

总结

  最开始没有想到翻转这个操作,结果搞得好麻烦,后来看了别人的帖子才知道翻转一下,感觉被自己的智商侮辱了。

标签:case,nyan,20,后缀,1077,lines,Sample,Kuchiguse,翻转
来源: https://blog.csdn.net/CV_Jason/article/details/86623862