【PAT(甲级)】1077 Kuchiguse

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

解题思路:

题目要求我们找出给出每一句中末尾相同的最长字符串。思路就很简单啦,把每一句都用string类型给存储下来,然后从后往前比较,一直到产生了不同的字符为止,把之前相同的字符输出即可。

易错点:

1. 注意一下,不要按照空格来划分单词,直接从后往前比较字符的相同与否即可;

2. 边界情况,一个字符都没有相同的时候,和所有字符都相同的时候,这两个情况是容易出错的;

3. 读取一行的代码用getline的时候,记得把前面读取数字后面的换行字符用getchar()消掉!

代码:

#include
using namespace std;
int main(){
	int N;
	cin>>N;
	string a[N];
	getchar();
	for(int i=0;i result;
	for(int i=1;i<=a[0].size();i++){
		int flag = 1;
		for(int j=0;j

你可能感兴趣的:(PAT(甲级),c++,pat考试)