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
题目的意思是寻找最大公共后缀,直接模拟一下。
// freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin); #include <iostream> #include <map> #include <queue> #include <stdio.h> #include <vector> #include <algorithm> #include <stack> #include <stdlib.h> #include <string.h> #include <math.h> #include <string> using namespace std; int n,mi; int num; struct E { int l; string s; }stu[300]; int p(int num){ int i,j; num=0; for(i=0;i<mi;i++){ for( j=1;j<n;j++){ if(stu[j].s[stu[j].l-i-1] != stu[j-1].s[stu[j-1].l-i-1]){ return num; } } num++; } return num; } int main(){ // freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin); int i; cin>>n; getchar(); mi=3000; for(i=0;i<n;i++){ getline(cin,stu[i].s); // cout<<stu[i].s<<endl; stu[i].l=stu[i].s.length(); if(stu[i].l<mi){ mi=stu[i].l; } } int ll; ll=p(0); if(ll==0)cout<<"nai"<<endl; else{ for(i=0;i<ll;i++) cout<<stu[1].s[stu[1].l-ll+i]; cout<<endl; } return 0; }