Description
This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.
You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions:
The length of t does not exceed the length of s.
t is a palindrome.
There exists two strings a and b (possibly empty), such that t=a+b ( “+” represents concatenation), and a is prefix of s while b is suffix of s.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤1000), the number of test cases. The next t lines each describe a test case.
Each test case is a non-empty string s, consisting of lowercase English letters.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 5000.
Output
For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.
Note
In the first test, the string s=“a” satisfies all conditions.
In the second test, the string “abcdfdcba” satisfies all conditions, because:
Its length is 9, which does not exceed the length of the string s, which equals 11.
It is a palindrome.
“abcdfdcba” = “abcdfdc” + “ba”, and “abcdfdc” is a prefix of s while “ba” is a suffix of s.
It can be proven that there does not exist a longer string which satisfies the conditions.
In the fourth test, the string “c” is correct, because “c” = “c” + “” and a or b can be empty. The other possible solution for this test is “s”.
题目大意
给出一个字符串s,
思路
如果本身就是回文串直接输出
否则答案就是s已经匹配的前缀后缀和剩下的回文前缀和后缀的最长的一条
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5;
string s;
bool ju(string &s,int l,int r){
while(l<=r&&s[l]==s[r])++l,--r;
return l>r;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>s;
int ls=s.size();
if(ju(s,0,ls-1)){
cout<<s<<endl;
continue;
}
int l=0,r=ls-1,r_,l_;
while(l<r&&s[l]==s[r])++l,--r;
for(int i=r;i>=l;--i)
if(ju(s,l,i)){
r_=i;
break;
}
for(int i=l;i<=r;++i)
if(ju(s,i,r)){
l_=i;
break;
}
cout<<s.substr(0,l);
cout<<(r_-l>r-l_?s.substr(l,r_-l+1):s.substr(l_,r-l_+1));
cout<<s.substr(r+1)<<endl;;
}
return 0;
}
困难版本和简单版思路其实大致相同,不过中间的匹配需要用manachar
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+5;
char tmp[maxn<<1];
int s1,s2,len[maxn<<1];
void manachar(string s){
s1=0,s2=0;
tmp[0]='$',tmp[1]='#';
int ls=s.size();
for(int i=0;i<2*ls+5;++i)len[i]=0;
for(int i=1;i<=ls;++i){
tmp[2*i]=s[i-1];
tmp[2*i+1]='#';
}
tmp[2*ls+2]='\0';
int mx=0,mid;
for(int i=1;tmp[i];++i){
if(i<mx)len[i]=min(len[2*mid-i],mx-i);
else len[i]=1;
while(tmp[i+len[i]]==tmp[i-len[i]])++len[i];
if(len[i]+i>mx){
mx=len[i]+i;
mid=i;
}
int l=len[i]-1;//匹配的原字符子串长度
if(i%2==0){//#a#
l=(l-1)/2;
int p=i/2;
if(p==l+1)s1=max(s1,len[i]-1);
if(p==ls-l)s2=max(s2,len[i]-1);
}else{//a#a
l/=2;
int p=(i-1)/2,q=(i+1)/2;
if(p==l)s1=max(s1,len[i]-1);
if(q==ls-l+1)s2=max(s2,len[i]-1);
}
}
}
int main()
{
int t;cin>>t;
while(t--){
string s,ss,ans1,ans2;
cin>>s;
int ls=s.size();
manachar(s);
if(s1>s2)ans1=s.substr(0,s1);
else ans1=s.substr(ls-s2);
int l=0,r=ls-1;
while(l<r&&s[l]==s[r])ans2+=s[l],++l,--r;
ss=s;
if(l<r){
ss=s.substr(l,r-l+1);
int lss=ss.size();
manachar(ss);
if(s1>s2)ans2+=ss.substr(0,s1);
else ans2+=ss.substr(lss-s2);
}
ans2+=s.substr(r+1);
if(ans1.size()>=ans2.size())cout<<ans1<<endl;
else cout<<ans2<<endl;
}
return 0;
}