题目大意:给一些字符串 求最大公共后缀子串
思路:
设置变量Count初始化0 记录每两串的公共后缀子串的长度
后每输入一个字符串 根据最小原则 更新Count
最后根据Count是否为0 来确定最终公共后缀子串
在这里我多此一举 将字符串倒转 然而 从后逐一判断 就可以了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int Max=1<<30;
int getCommonLen(string S1, string S2)
{
int CL=0;
int size=(S1.size()<S2.size() ? S1.size():S2.size());
for(int i=0; i<size; i++)
{
if(S1[i]==S2[i])
CL++;
else
return CL;
}
return CL;
}
int main()
{
int T,Count=Max,flag=0;
string S1, S2;
cin>>T;
getchar();
getline(cin, S1);
getline(cin, S2);
reverse(S1.begin(),S1.end());
reverse(S2.begin(),S2.end());
Count=(getCommonLen(S1,S2)<Count ? getCommonLen(S1,S2):Count);
if(Count==0)
flag=1;
T-=2;
while(T--)
{
getline(cin,S1);
if(!flag){
reverse(S1.begin(),S1.end());
Count=(getCommonLen(S1,S2)<Count ? getCommonLen(S1,S2):Count);
if(Count==0)
flag=1;
S2=S1;
}
}
if(flag)
cout<<"nai"<<endl;
else
{
S2=S2.substr(0,Count);
reverse(S2.begin(),S2.end());
cout<<S2<<endl;
}
return 0;
}