首先可以
O(n)
O
(
n
)
的预处理出所有的不合法位置
然后我的第一想法是用我的长度去包含所有的不合法位置,可能会有若干种包含方法,然后发现只要满足一个什么什么样的字符串哈希值不同就行
但这样的考虑忽略了一个特性:我的覆盖是可以从尾巴出发,穿越到头再覆盖的,这个比较难处理
不妨换一个角度:我们预处理出了若干的不合法位置,把原串分割成了若干个合法的小串,我们最后剩下的合法字符串一定是某一个小串的子串
我们枚举每一个小串,设该小串的长度为l
假设我们要在这个小串中挖一个长度为len1的子串作为答案,那么它对应的delete长度就是len-len1,其中len是原串长度
这样的子串从头到尾都是符合题意的,唯一的问题是最后一个可能和第一个相等
那么对于所有的长度为len1的子串,只要
s[1]!=s[len1]
s
[
1
]
!
=
s
[
l
e
n
1
]
,
s[2]!=s[len1+1]
s
[
2
]
!
=
s
[
l
e
n
1
+
1
]
…中的任意一个满足,就存在一个合法的长度为len1的子串,我们发现这个东西合并起来就是判断该串的首起len-len1+1子串与末起len-len1+1子串是否相同,这个可以用哈希预处理
写的时候还有一些小细节:比如可以提前将字符串复制一遍贴在后面,以节省循环的讨论,还有要从一个不合法的位置作为原串的开始,这样可以保证原串的开头和结尾被接了起来
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cctype>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int,int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;
const int MOD=998244353;
const LL LINF=2e16;
const int INF=2e9;
const int magic=348;
const double eps=1e-10;
const double pi=3.14159265;
inline int getint()
{
char ch;int res;bool f;
while (!isdigit(ch=getchar()) && ch!='-') {}
if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
while (isdigit(ch=getchar())) res=res*10+ch-'0';
return f?res:-res;
}
int len;
char s[2000048];
namespace Hash
{
const int p=19260817;
LL hsh[2000048],pw[2000048];
inline void init()
{
int i;pw[0]=1;
for (i=1;i<=len*2;i++)
{
pw[i]=pw[i-1]*p;
hsh[i]=hsh[i-1]*p+s[i];
}
}
inline LL gethash(int left,int right)
{
return hsh[right]-hsh[left-1]*pw[right-left+1];
}
}
bool ans[1000048];
int main ()
{
int i,ca,lastpos,curpos,left,right,curlen;ca=getint();
while (ca--)
{
scanf("%s",s+1);len=strlen(s+1);
for (i=len+1;i<=len*2;i++) s[i]=s[i-len];
Hash::init();memset(ans,false,sizeof(ans));
for (i=1;i<=len && s[i]!=s[i+1];i++) {}
if (i>len)
{
for (curlen=2;curlen<=len;curlen++)
if (Hash::gethash(1,len-curlen+1)!=Hash::gethash(curlen,len)) ans[len-curlen]=true;
for (i=0;i<=len-2;i++) if (ans[i]) printf("1"); else printf("0");
printf("1\n");continue;
}
lastpos=i;curpos=i+1;
while (curpos<=i+len-1)
{
while (curpos<=i+len-1 && s[curpos]!=s[curpos+1]) curpos++;
left=lastpos+1;right=curpos;
for (curlen=2;curlen<=right-left+1;curlen++)
if (Hash::gethash(left,right-curlen+1)!=Hash::gethash(left+curlen-1,right))
ans[len-curlen]=true;
lastpos=curpos;curpos++;
}
for (i=0;i<=len-2;i++) if (ans[i]) printf("1"); else printf("0");
printf("1\n");
}
return 0;
}