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

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

解答

#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;
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论