这是这个问题的困难版本。区别在于对字符串长度和测试用例数量的限制。只有当你解决了这个任务的所有版本,你才能做黑客。给你一个字符串s,由小写英文字母组成。找出满足下列条件的最长字符串t:t的长度不超过s的长度。T是一个回文。存在两个字符串a和b(可能为空),使得t = a +b(“+”表示连接),a是s的前缀,而b是l s。输入输入由多个测试用例组成。第一行包含一个整数t (1 < t < 105),测试用例的数量。接下来的t行分别描述了一个测试用例。每个测试用例都是非空字符串s,由小写英文字母组成。可以保证所有测试用例的字符串长度之和不超过106。输出对于每个测试用例,打印满足上述条件的最长字符串。如果存在多个可能的解决方案,打印其中任何一个。
Example
input
Copy
5 a abcdfdcecba abbaxyzyx codeforces acbba
output
Copy
a abcdfdcba xyzyx c abba
请注意在第一个测试中,字符串s ="a"满足所有条件。在第二个测试中,字符串"abcdfdcba"满足所有条件,因为:它的长度是9,不超过字符串s的长度,s等于11。这是一个回文。"abcdfdcba" = "abcdfdc" + "ba",其中"abcdfdc"是s的前缀,"ba"是s的后缀。可以证明不存在满足这些条件的更长的弦。在第四个测试中,字符串“c”是正确的,因为“c”=“c”+“”,a或b可以为空。这个测试的另一个可能的解决方案是“s”.
题解:
首先对于结尾首尾相同的不用管,答案无影响,一直遍历到sl != sr
我们设l ~ r这一段为t
求得关键是以t首或尾为开头的最长回文串
如果用马拉车的话,中间需要一点判断,来记录我们求的回文串是从开头开始的,对于尾的话,把t反转,再用一次马拉车即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define int long long
int len[2000050];
char p[2000050];
string mana(string t)
{
int n = t.size();
p[0] = '#';
int m = 0;
t = " " + t;
for(int i = 1;i <= n;i++)
{
p[++m] = t[i];
p[++m] = '#';
}
p[++m] = 0;
int ma = 0;
int mid = 0;
int ans = 0;
for(int i = 0;i < m;i++)
{
if(i < ma)
{
len[i] = min(len[mid*2 - i],ma - i);
}
else
{
len[i] = 1;
}
while(i - len[i] >= 0&&i + len[i] <= m&&p[i - len[i]] == p[i + len[i]])
len[i]++;
if(i + len[i] > ma)
{
ma = i + len[i];
mid = i;
}
int L = i/2 + 1 - (len[i] - 1)/2;//关键一步,判断是否从头开始
string now;
if(L == 1)
{
ans = max(ans,len[i] - 1);
}
}
string now ;
for(int i = 1;i <= ans;i++)
{
now += t[i];
}
return now;
}
void solve()
{
string s;
cin >> s;
int n = s.size();
s = " " + s;
int l = 1,r = n;
while(l < r&&s[l] == s[r])
{
l++;
r--;
}
string t;
for(int i = l;i <= r;i++)
t += s[i];
string x = mana(t);
reverse(t.begin(),t.end());
string y = mana(t);
for(int i = 1;i < l;i++)
cout << s[i];
if(x.size() > y.size())
cout <<x;
else
cout <<y;
for(int i = r + 1;i <= n;i++)
cout <<s[i];
cout <<"\n";
}
signed main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int t = 1;
cin >> t;
while(t--)
{
solve();
}
}
还有一种解法,是通过kmp,求比马拉车更便捷,我们把字符串t + '&' + t的反转得到一个新字符串
考虑到kmp的定义,我们可以求出,首尾最长相似串,
同样t反转,再来一次
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vec;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
inline void out(int x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 1e6 + 10;
int nextn[maxn << 1];
string s, str;
int t, x;
string solve(string str) {
s = str;
reverse(s.begin(), s.end());
s = str + '$' + s;
nextn[0] = -1;
int j = -1, i = 0;
while (i < s.size()) {
while (j != -1 && s[j] != s[i]) j = nextn[j];
nextn[++i] = ++j;
}
return s.substr(0, j);
}
int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
int k = 0;
cin >> str;
for (int i = 0; i < str.size(); i++) {
if (str[i] == str[str.size() - i - 1]) k++;
else break;
}
if (k + 1 > str.size()) {
cout << str << endl;
continue;
}
int cnt = 1;
string ss = str.substr(k, str.size() - 2 * k);
string x = solve(ss);
reverse(ss.begin(), ss.end());
string xx = solve(ss);
for (int i = 0; i < k; i++) cout << str[i];
if (xx.size() > x.size()) cout << xx;
else cout << x;
for (int i = k - 1; i >= 0; i--) cout << str[i];
cout << endl;
}
return 0;
}