# [BZOJ]2806: [Ctsc2012]Cheat

$$dp[i]=max(dp[i-1],max(i-j+dp[j])\left ( i-L\geq j\geqslant i-num[i] \right ))$$

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=2e6+10;
const double eps=1e-8;
#define ll long long
using namespace std;
struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}

int ch[MAXN][2],fa[MAXN],dis[MAXN];
int cnt,cur,rt;
void built(int x){
int last=cur;cur=++cnt;dis[cur]=dis[last]+1;int p=last;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=cur;
if(!p)fa[cur]=rt;
else{
int q=ch[p][x];
if(dis[q]==dis[p]+1)fa[cur]=q;
else{
int nt=++cnt;dis[nt]=dis[p]+1;
memcpy(ch[nt],ch[q],sizeof(ch[q]));
fa[nt]=fa[q];fa[q]=fa[cur]=nt;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nt;
}
}
}

char s[MAXN];
int num[MAXN];
void ycl(char str[]){
int len=strlen(str);int len1=0;int temp=rt;
for(int i=0;i<len;i++){
int t=str[i]-'0';
if(ch[temp][t])len1++,temp=ch[temp][t];
else{
int p=temp;
for(;p&&!ch[p][t];p=fa[p]);
if(!p)temp=rt,len1=0;else temp=ch[p][t],len1=dis[p]+1;
}
num[i]=len1;
}
}

int dp[MAXN];
int st[MAXN],totl,totr;

bool check(int L,int n){
for(int i=0;i<n;i++)dp[i]=0;
totl=1;totr=0;
for(int i=L-1;i<n;i++){
dp[i]=dp[i-1];
int t=i-L;
while(totr>=totl&&dp[t]-t>=dp[st[totr]]-st[totr])totr--;
st[++totr]=t;
while(totl<=totr&&st[totl]<i-num[i])totl++;
if(totl<=totr)dp[i]=max(dp[i],i+dp[st[totl]]-st[totl]);
}
return (dp[n-1]*10>=9*n);
}

int main(){
int R=0;
rt=cur=cnt=1;
while(m--){
cur=1;scanf("%s",s);
int len=strlen(s);R=max(R,len);
for(int i=0;i<len;i++)built(s[i]-'0');
}
while(n--){
scanf("%s",s);
int l=1;int r=R;int ans=0;
ycl(s);int len=strlen(s);
while(l<=r){
int mid=(l+r)>>1;
if(check(mid,len))ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}


## 2806: [Ctsc2012]Cheat

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 1877  Solved: 973
[Submit][Status][Discuss]

## Output

N行，每行一个整数，表示这篇作文的Lo 值。

1 2
10110
000001110
1011001100

4

## HINT

posted @ 2019-02-07 00:22  wang9897  阅读(203)  评论(0编辑  收藏  举报