#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#ifdef WIN32
#define fmt64 "%I64d"
#else
#define fmt64 "%lld"
#endif
#define oo 0x13131313
using namespace std;
template<class T> inline bool minim(T &a, const T &b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool maxim(T &a, const T &b) {return b > a ? a = b, 1 : 0;}
template<class T> inline T sqr(const T &a) {return a * a;}
typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
#define maxn 1100005
struct trie
{
trie *c[3], *f;
int l;
}root[maxn * 2], *tt = root, *prev = root;
int n, m;
void build()
{
int k = 0, c;
for (; ~(c = getchar()); ) {
if (c == '\n') if (++k == m) break; else c = '2';
c -= '0';
trie *p = ++tt, *q, *t; p->l = prev->l + 1;
for (q = prev; q && !q->c[c]; q = q->f) q->c[c] = p;
if (!q) p->f = root;
else if (q->c[c]->l == q->l + 1) p->f = q->c[c];
else {
*(++tt) = *q->c[c], tt->l = q->l + 1, q->c[c]->f = p->f = tt;
for (t = q->c[c]; q && q->c[c] == t; q = q->f) q->c[c] = tt;
}
prev = p;
}
}
int len, f[maxn], q[maxn], d[maxn]; char st[maxn];
void get_dist(char st[])
{
int i, j = 0; trie *p = root;
for (i = 1; st[i]; ++i) {
int c = st[i] - '0';
if (p->c[c]) ++j, p = p->c[c];
else {
for (; p && !p->c[c]; p = p->f);
if (!p) p = root, j = 0; else j = p->l + 1, p = p->c[c];
}
d[i] = j;
}
len = i - 1;
}
int dp(int L)
{
int i, j, *ll = q + 1, *rr = q;
for (i = 0; i < L; ++i) f[i] = 0;
for (i = L; i <= len; ++i) {
f[i] = f[i - 1];
for (j = i - L; ll <= rr && f[*rr] - *rr < f[j] - j; --rr);
*(++rr) = j;
for (; ll <= rr && *ll < i - d[i]; ++ll);
if (ll <= rr) maxim(f[i], f[*ll] - *ll + i);
}
return f[len];
}
void work()
{
gets(st + 1), get_dist(st);
int l = 1, r = len + 1;
for (; l < r; ) {
int mid = (l + r) >> 1;
int k = dp(mid);
if (k / (double)len > 0.89999999999) l = mid + 1; else r = mid;
}
printf("%d\n", l - 1);
}
int main()
{
freopen("2806.in", "r", stdin);
freopen("2806.out", "w", stdout);
//fprintf(stderr, "%d\n", (int)ceil(10.1));
scanf("%d%d\n", &n, &m);
build();
for (; n--; ) work();
return 0;
}
后缀自动机+dp
[ctsc2012] cheat
最新推荐文章于 2018-12-15 10:26:00 发布