PAT甲级1077,字符串

#include
using namespace std;

int main(){
    int n;
    string s1;
    cin>>n;
    getchar();//吸收换行符
    getline(cin,s1);

    for(int k=1;k<n;k++){
        string s2;
        getline(cin,s2);
        int i=s1.length()-1;
        int j=s2.length()-1;
        string temp;

        while(i>=0 && j>=0){
            if(s1[i]==s2[j])
                temp.push_back(s1[i]);
            else
                break;
            i--;
            j--;
        }

        s1=temp;//让s1等于temp,以便之后再次进行和s2的比较
        reverse(s1.begin(),s1.end());
    }

    if(s1.size()!=0)
        cout<<s1<<endl;
    else
        cout<<"nai"<<endl;
    return 0;
}

你可能感兴趣的:(PAT甲级)