AcWing 779. 最长公共字符串后缀
原题链接
简单
作者:
hh_88
,
2024-10-06 16:05:55
,
所有人可见
,
阅读 2
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin>>n,n!=0)
{
string a[n];
int location=-1;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if(n==1) {cout<<a[0];return 0;}//*****n=1时直接输出就可
for(int i=0;i< n-1;i++)
{
if(n==1) {string f=a[0];location=f.size();cout<<location;break;}
string b ,c;
b=a[i];c=a[(i+1)%n];
reverse(b.begin(),b.end());
reverse(c.begin(),c.end());
int current_location=-1;
for(int j=0;j<min(b.size(),c.size());j++)
{
if(b[j]==c[j]) current_location=j;
else break;
}
if(current_location<location||location==-1)//寻找最短前缀输出
{
location=current_location;
}
}
if(location<0) cout<<endl;
else
{
string result =a[0];
reverse(result.begin(),result.end());
result = result.substr(0,location+1);
reverse(result.begin(),result.end());
cout<<result<<endl;
}
}
}