1077 Kuchiguse (20分)

1077 Kuchiguse (20分)

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<iostream>
#include<algorithm>
using namespace std;
int n,num;
string m[110],p;

bool judge(int k)
{
    if(k>=num)
        return false;
    char c=m[0][k];
    for(int i=1;i<n;i++)
        if(m[i][k]!=c)
            return false;
    return true;
}
int main()
{
    cin >> n;
    getchar();
    for(int i=0;i<n;i++)
    {
        getline(cin,p);
        reverse(p.begin(),p.end());
        m[i]=p;
        if(i==0)
            num=p.size();
        else
            if(p.size()<num)
                num=p.size();
    }
    string ans="";
    bool flag=true;
    int k=0;
    while(flag)
    {
        if(judge(k))
        {
            ans+=m[0][k];
            k++;
        }
        else
            flag=false;
    }
    if(ans.size()!=0)
        for(int i=ans.size()-1;i>=0;i--)
            printf("%c",ans[i]);
    else    
        printf("nai");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章