pat 1077. Kuchiguse (20)

1077. Kuchiguse (20)

时间限制
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
  • 解:蛮费事的,虽然思路很常规,因为字符串的个数不多,所以全部逆序处理了一遍,提交的时候有一组样例一路卡,还好自己找到卡的位置了,输入3 tob uob tob,应该输出ob,最后发现循环写的有点问题,改了下总算过了。

    代码:

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main()
    {
        int n;
    lp: while(scanf("%d",&n)==1)
        {
            string *s=new string[n+1];
            int minlen=257;
            getchar();
            for(int i=0;i<n;i++)
            {
                getline(cin,s[i]);
                if(s[i].length()<minlen) minlen=s[i].length();
            }
            //重开数组
            string *t=new string[n+1];
            for(int i=0;i<n;i++)
            {
                for(int j=s[i].length()-1;j>=0;j--)
                {
                    t[i].push_back(s[i][j]);
                }
            }
            string res;
            if(minlen==0)
            {
                printf("nai\n");
                goto lp;
            }
            if(minlen==1)
            {
                for(int j=1;j<n;j++)
                {
                    if(t[j][0]!=t[0][0])
                    {
                        printf("nai\n");
                        goto lp;
                    }
                }
                printf("%c\n",t[0][0]);
                goto lp;
            }
            for(int i=1;i<minlen;i++)
            {
                string hhd=t[0].substr(0,i);
                int flag=1;
                for(int j=1;j<n;j++)
                {
                    if(t[j].substr(0,i)!=hhd)
                    {
                        if(i==1)
                        {
                            printf("nai\n");
                            goto lp;
                        }
                        flag=0;
                        break;
                    }
                }
                if(flag==1)
                {
                    res=hhd;
                }
                else
                {
                    res=t[0].substr(0,i-1);
                    break;
                }
            }
            for(int i=res.length()-1;i>=0;i--)
                    cout<<res[i];
            printf("\n");
            goto lp;
        }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    posted on 2015-07-23 21:10  Tob__yuhong  阅读(155)  评论(0编辑  收藏  举报

    导航