The topics are as follows:
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 are often exaggerated artistically in anime and Manga. For example, the artificial sentence ending particle "nyan~" are 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 I find her kuchiguse?
Input Specification:
Each input file contains the one test case. For each case, the first line was an integer N (2<=n<=100). Following is N file lines of 0~256 (inclusive) characters in length, each representing a character ' s spoken line. The spoken lines is case sensitive.
Output Specification:
For each test case, print on 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:3Itai Nyan~ninjin wa iyadanyan~uhhh nyan~
Sample Output 1:nyan~
Sample Input 2:3itai! Ninjinnwaiyada t_tt_t
Sample Output 2:Nai
The topic asks for a common end from a given string of strings, because this end will be shared by all strings, so we save the first input string, use it to compare with each string, find the common location, and once we find that there is no common part, return to Nai immediately, because the input and output streams are separate, So we returned the output file in advance without any problems; if we find the public part, we need to save it, we use a suffix variable to save the common string, when the common part length is smaller than suffix, the public part is shortened, should be updated, To ensure that the first public part can write to suffix, we initialize the value of suffix to the first input.
For input with spaces, you should use Getline (),getline () has a problem is to eat the last line of the carriage return as a blank line, so Getline () before the input must be GetChar () eat that carriage return to deal with .
The code is as follows:
#include <iostream> #include <stdio.h> #include <string> #include <vector>using namespace std; int main () { int N; string Temp,suffix,input; Cin >> N; GetChar (); Getline (cin,temp); suffix = temp; for (int i = 1; i < N; i++) { getline (cin,input); int tempcur = Temp.length ()-1; int inputcur = Input.length ()-1; while (input[inputcur] = = Temp[tempcur]) { inputcur--; tempcur--; } int len = temp.length ()-tempCur-1; if (len = = 0) { suffix = "nai"; break; } if (Suffix.length () > len) { suffix = temp.substr (tempcur + 1); } } cout << suffix << endl; return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1077. Kuchiguse (20)