PAT A1077 Kuchiguse (20)

总结:

本来正确的思路,却搞了好久才弄出来,自己给自己挖坑。公共后缀的长度肯定是从(1-最短的一个字符串长),我却慢慢改,认为8个字符就后了,或者大于等于2才行,哎!以后思维一定慎密啊!

代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool strzz(string s, string& flag)
{
    int st = 0; string yyy;
    for (int i = s.length() - 1,j=0; i >= 0 && j < flag.length(); i--,j++)
    {
        if (s[i] == flag[j]){ st++; yyy.push_back(flag[j]); }
        else break;
    }
    if (st >= 2){ flag = yyy; return true; }
    else return false;
}
int main()
{
    int m,n=0;
    int ct=1;
    cin >> m; getchar();
    string flag;
    for (int i = 0; i < m; i++)
    {
        string s;
        getline(cin,s);
        if (i == 0)        
        {
            for (int i = s.length() - 1; i >= 0; i--)
                flag.push_back(s[i]);
        }
        else if (strzz(s, flag))n++;
        else ct = 0;
    }
    if (ct == 0)cout << "nai";
    else {
        string gh;
        for (int i = flag.length()-1; i >= 0; i--)
            gh.push_back(flag[i]);
        cout << gh;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/81388647