标签:space set 链接 while lin https force tps name
题目链接
manacher做法
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int p[N*2];
string manacher(string ss)
{
int len = ss.size();
string s;
s.resize(len*2+2);
for(int i=len;i>=0;i--)
{
s[i*2+2] = ss[i];
s[i*2+1] = ‘#‘;
}
s[0] = ‘*‘;
int maxlen = 0,id = 0;
for(int i=2;i<=len*2;i++)
{
if(p[id]+id>i)
p[i] = min(p[id*2-i],p[id]+id-i);
else
p[i] = 1;
while(s[i+p[i]]==s[i-p[i]])
p[i]++;
if(p[id]+id<p[i]+i)
id = i;
if(p[i]==i)
maxlen = p[i]-1;
}
return ss.substr(0,maxlen);
}
int main()
{
string s;
cout << s;
int T;
cin >> T;
while(T--)
{
cin >> s;
int l = 0 ,r = s.size()-1;
string ans;
while(s[l]==s[r])
{
l++;
r--;
}
if(l>r)
{
cout << s <<"\n";
continue;
}
string ss = s.substr(l,s.size()-2*l);
string s1 = manacher(ss);
reverse(ss.begin(),ss.end());
string s2 = manacher(ss);
ans = s.substr(0,l);
if(s1.size()>s2.size())
{
ans += s1;
}
else
{
ans += s2;
}
ans += s.substr(s.size()-l,l);
cout << ans << "\n";
}
return 0;
}
Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version) -- manacher
标签:space set 链接 while lin https force tps name
原文地址:https://www.cnblogs.com/hh13579/p/12576369.html