problem list
G 忘记密码
- 非常魔性的一道题,然而看着题解还是有5个样例没过,那我也是没有一点办法诶
- 我是用的后缀自动机做的,然并卵,就当是新学一个自动机的姿势吧~
- 顺便求好心人指点一二怎样改代码或晒下AC原码~
- 上题解:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 300000 + 10;
const int inf = 0x3f3f3f3f ;
const int npos = -1 ;
const double eps = 1e-20 ;
struct Suffix_Automata{
struct State{
int val;
State *suf, *go[26];
State() : suf(0), val(0){
memset(go,0,sizeof(go));
}
};
State *root, *last;
State statePool[maxn*2], *cur;
void init(){
cur=statePool;
root=last=cur++;
}
void extend(int w){
State *p=last, *np=cur++;
np->val=p->val+1;
while(p && !p->go[w]){
p->go[w]=np;
p=p->suf;
}
if(!p){
np->suf=root;
}else{
State *q=p->go[w];
if(p->val+1 == q->val){
np->suf=q;
}else{
State *nq=cur++;
memcpy(nq->go,q->go,sizeof(q->go));
nq->val=p->val+1;
nq->suf=q->suf;
q->suf=nq;
np->suf=nq;
while(p && p->go[w]==q){
p->go[w]=nq;
p=p->suf;
}
}
}
last=np;
}
void LCS(char *str, int *rec){
int lth=strlen(str);
int ans=0, len=0;
State *p=root;
for(int i=0;i<lth;i++){
if(p->go[str[i]-'a']){
len++;
ans=max(ans,len);
p=p->go[str[i]-'a'];
}else{
while(p && !p->go[str[i]-'a']){
p=p->suf;
}
if(!p){
p=root;
len=0;
}else{
len=p->val+1;
ans=max(len,ans);
p=p->go[str[i]-'a'];
}
}
rec[i]=len;
rec[lth]=ans;
}
}
void cal(char *s1, char *s2, int *rec){
init();
int lth=strlen(s1);
for(int i=0;i<lth;i++){
extend(s1[i]-'a');
}
LCS(s2,rec);
}
};
char T[maxn], S[11][maxn];
int common[11][maxn], idx[11];
int n, k, mk, lth;
Suffix_Automata ASM;
int main(){
while(~scanf("%s",T)){
k=inf;
lth=strlen(T);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",S[i]);
memset(&ASM,0,sizeof(ASM));
ASM.cal(S[i],T,common[i]);
k=min(k,common[i][lth]);
}
int l=1, r=k, ans=0;
while(l<=r){
k=(l+r)>>1;
int pre=-1, now, flag=1, no=1;
for(int i=1;i<=n;i++){
no=1;
int li=pre+k, ri=lth-1;
for(int j=li;j<=ri;j++){
if(k==common[i][j]){
no=0;
pre=j;
break;
}
}
if(no){
flag=0;
break;
}
}
if(flag){
ans=max(ans,k);
l=k+1;
}else{
if(1==(r-l)){
l=r;
}else{
r=k-1;
}
}
}
printf("%d\n",ans?ans:-1);
}
return 0;
}