1077. Kuchiguse (20)

1.题目要求求每个句子后面的公共字符串,如果有则输出,如果没有则输出nai。

2.需要使用getline输入,避免空格断开。

3.其他没有太多特点的地方。

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

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

AC代码:

//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;

int main(void)
{
	string nStr;
	getline(cin, nStr);
	int n = 0;
	for (int i = 0; i < nStr.size(); i++)
		n = n * 10 + nStr[i] - '0';
	vector<string> sentence(n);
	int minLen = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		getline(cin,sentence[i]);
		minLen = min(minLen,(int) sentence[i].size());
	}
	string ans = "";
	bool diff = false;
	for (int i = 1; i <= minLen; i++)
	{
		char c = sentence[0][sentence[0].size() - i];
		for (int j = 1; j < sentence.size(); j++)
		{
			if (c != sentence[j][sentence[j].size() - i])
			{
				diff = true;
				break;
			}
		}
		if (diff) break;
		else ans = c + ans;
	}
	if (ans.size() == 0)
		cout << "nai" << endl;
	else
		cout << ans << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注