# BZOJ2806 [Ctsc2012]Cheat 【后缀自动机 + 二分 + 单调队列优化DP】

## 输出格式

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

1 2

10110

000001110

1011001100

4

## 题解

【一定要好好写队列QAQ，WA了几发】

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 3000005,maxm = 100005,INF = 1000000000;
int pre[maxn],ch[maxn][3],step[maxn],cnt,last;
char s[maxn];
void ins(int x){
int p = last,np = ++cnt;
last = np; step[np] = step[p] + 1;
while (p && !ch[p][x]) ch[p][x] = np,p = pre[p];
if (!p) pre[np] = 1;
else {
int q = ch[p][x];
if (step[q] == step[p] + 1) pre[np] = q;
else {
int nq = ++cnt; step[nq] = step[p] + 1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
pre[nq] = pre[q]; pre[q] = pre[np] = nq;
while (ch[p][x] == q) ch[p][x] = nq,p = pre[p];
}
}
}
bool check(int L){
head = tail = f[0] = 0;
for (int i = 1; i <= N; i++){
f[i] = f[i - 1];
int l = i - len[i],r = i - L;
if (r >= 0){
while (head < tail && f[q[tail - 1]] - q[tail - 1] < f[r] - r) tail--;
q[tail++] = r;
}
}
return 10 * f[N] >= 9 * N;
}
void init(){
int u = 1,id,ans = 0;
for (int i = 1; i <= N; i++){
id = s[i] - '0';
if (ch[u][id]) len[i] = ++ans,u = ch[u][id];
else {
while (u != 1 && !ch[u][id]) u = pre[u];
if (u == 1) len[i] = ans = 0;
else len[i] = ans = step[u] + 1,u = ch[u][id];
}
}
}
int main(){
int n,m; last = cnt = 1;
scanf("%d%d",&n,&m);
while (m--){
scanf("%s",s); N = strlen(s);
for (int i = 0; i < N; i++) ins(s[i] - '0');
ins(2);
}
while (n--){
scanf("%s",s + 1); N = strlen(s + 1);
init();
int l = 0,r = N,mid,ans = 0;
while (l <= r){
mid = l + r >> 1;
if (check(mid)) l = mid + 1,ans = mid;
else r = mid - 1;
}
printf("%d\n",ans);
}
return 0;
}


posted @ 2018-01-18 14:08  Mychael  阅读(146)  评论(0编辑  收藏  举报