Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

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

Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1


Sample Input 2

Ninjinnwaiyada T_T

Sample Output 2



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
	int n, len[110]; char s[110][300];
	scanf("%d", &n); getchar();
	for (int i = 0; i < n; i++) gets(s[i]), len[i] = strlen(s[i]);

	int mn = *min_element(len, len + n), i;
	for (i = 0; i < mn; i++)
		bool same = 1;
		for (int j = 1; j < n; j++)
			if (s[j][len[j]-1-i] != s[0][len[0]-1-i])
				same = 0; break;
		if (!same) break;

	printf("%s\n", i ? s[0] + len[0] - i : "nai");
	return 0;
上一篇: 下一篇: