题目大意:
就是对于一个串, 长度不超过3*10^5, 询问其中最长的形似是ww^ww^类型的串, 其中w^是w反转得到的串
大致思路:
好早以前写的题了....当时交UVALive一直返回WA一直找不到错哪里了...现在回去看发现Rejudge成AC了....
就是先Manacher处理出每个位置的回文半径
从左至右扫一遍用set维护当前能覆盖到的右界, 然后枚举后半的ww^的中心应该选取的最左的左中心即可
代码如下:
Result : Accepted Memory : ? KB Time : 1996 ms
/*
* Author: Gatevin
* Created Time: 2015/5/29 10:02:17
* File Name: Rin_Tohsaka.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
#define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl
char in[300010];
char s[600010];
int R[600010];
void Manacher(char *s, int *R, int len)
{
int p = 0, mx = 0;
R[0] = 1;
for(int i = 1; i <= len; i++)
{
if(mx > i) R[i] = min(R[2*p - i], mx - i);
else R[i] = 1;
while(s[i - R[i]] == s[i + R[i]])
R[i]++;
if(i + R[i] > mx)
p = i, mx = i + R[i];
}
return;
}
set<pair<int, int> > S;
set<pair<int, int> > S2;
vector < pair<int, int> > V;
int main()
{
int Z;
scanf("%d", &Z);
while(Z--)
{
scanf("%s", in);
int len = strlen(in);
s[0] = '@';
for(int i = 0; i < len; i++)
s[2*i + 1] = in[i], s[2*i + 2] = '#';
s[2*len] = '$';
Manacher(s, R, 2*len);
/*
* 枚举ww^ww^的中心位置为i,
* 那么对于i是最优解的位置ww^的对称中心j满足覆盖半径R[j]: j + R[j] - 1 >= i - 1, j肯定也是偶数
*/
//for(int i = 0; i <= 2*len; i++)
// cout<<R[i]<<endl;
S.clear();
S2.clear();
int ans = 0;
for(int i = 2; i < 2*len; i += 2)
{
V.clear();
set<pair<int, int> > :: iterator it;
for(it = S.begin(); it != S.end(); it++)
if((*it).first < i - 1) V.push_back(make_pair((*it).first, (*it).second));
else break;
for(int j = 0, sz = V.size(); j < sz; j++)
S.erase(V[j]), S2.erase(make_pair(V[j].second, V[j].first));
//2*y - i + 1 >= i - R[i] + 1
//即 y >= (2*i - R[i]) / 2
it = S2.lower_bound(make_pair(((2*i - R[i]) >> 1) + (R[i] & 1), 0));
if(it != S2.end())
ans = max(ans, (i - (*it).first) << 1);
S.insert(make_pair(i + R[i] - 1, i));
S2.insert(make_pair(i, i + R[i] - 1));
}
printf("%d\n", ans);
}
return 0;
}