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:
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_TSample Output 2:
nai
#include <string>#include <vector>#include <iostream>#include <stdio.h>#pragma warning(disable:4996)using namespace std;vector<char> kuchi;vector<string> s;int main(void) {int n;cin >> n;char ctemp;scanf("%c", &ctemp);for (int i = 0; i < n; i++) {string temp;while (true){scanf("%c", &ctemp);if (ctemp != ‘\n‘)temp += ctemp;elsebreak;}s.push_back(temp);}int p = 1;char c;bool flag = true;if (s[0].length() == 0) {cout << "nai";return 0;}while (true){c = s[0][s[0].length() - p];for (int i = 1; i < n; i++) {if (s[i].length() < p) {flag = false;break;}if (s[i][s[i].length() - p] != c) {flag = false;break;}}if (flag == false)break;kuchi.push_back(c);p++;}if (kuchi.size() == 0) {cout << "nai" << endl;}else {string kuchigusei;for (int i = kuchi.size() - 1; i >= 0; i--)kuchigusei += kuchi[i];cout << kuchigusei;}return 0;}
原文:http://www.cnblogs.com/zzandliz/p/5023248.html