NOI2018 你的名字

这个lk菜爆了

线段树都能写错

luogu
loj

68pts

TSSAM中匹配得到每个前缀最长在S中出现的后缀,长度为f[i],然后对TSAM,每加入一个T[i]trans(start,T[1:i])=t,对答案的贡献就是min(maxlen(t)-maxlen(link(t)),maxlen(t)-f[i])=maxlen(t)-max(maxlen(link(t)),f[i])

100pts

每次S只是原来S的子串。

用可持久化线段树求出每个状态的endpos集合。

设当前匹配上的串是W

每次W=W+c的时候,只要W不在S中,就把W第一个字符删掉。

W长度为len,每次成功匹配len最多+1,最多匹配成功|T|次,所以-1也不超过|T|次。时间复杂度就是O(|T|log|S|),其中log|S|是查询的复杂度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Author: QAQ-Automaton
LANG: C++
PROG: name.cpp
Mail: cnyalilk@vip.qq.com
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define inf 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
// input a signed integer
inline bool read (signed &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}

inline bool read (long long &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
x=gc();
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r')if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
// print a signed integer
inline bool write (signed x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}

inline bool write (long long x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
char s[500005],t[500005];
int rt[1000005],ls[20000005],rs[20000005],c,n;
void merge(int &x,int y){
if(!y)return;
if(!x){x=y;return;}
++c;
ls[c]=ls[x];rs[c]=rs[x];
x=c;
merge(ls[x],ls[y]);
merge(rs[x],rs[y]);
}
int insert(int x,int l=1,int r=n){
int cur=++c;
if(l==r)return cur;
int mid=(l+r)>>1;
if(x<=mid)ls[cur]=insert(x,l,mid);
else rs[cur]=insert(x,mid+1,r);
return cur;
}
int query(int x,int l,int r,int sl=1,int sr=n){
if(!x)return 0;
if(l<=sl && sr<=r)return 1;
int mid=(sl+sr)>>1;
if(l<=mid)if(query(ls[x],l,r,sl,mid))return 1;
if(mid<r)if(query(rs[x],l,r,mid+1,sr))return 1;
return 0;
}
namespace A{
int to[1000005][26],fail[1000005],len[1000005],now=1,t=1,l;
void extend(int x){
int nw=++t,i=now;
len[nw]=++l;
for(;i&&!to[i][x];i=fail[i])to[i][x]=nw;
if(!i)fail[nw]=1;
else if(len[i]+1==len[to[i][x]])fail[nw]=to[i][x];
else{
int o=to[i][x],n=++t;
len[n]=len[i]+1;
fail[n]=fail[o];
for(int j=0;j<26;++j)to[n][j]=to[o][j];
fail[o]=fail[nw]=n;
for(;i&&to[i][x]==o;i=fail[i])to[i][x]=n;
}
now=nw;
rt[nw]=insert(l);
}
int a[1000005],b[1000005];
void work(){
for(int i=1;i<=t;++i)++a[len[i]];
for(int i=1;i<=l;++i)a[i]+=a[i-1];
for(int i=t;i;--i){
b[a[len[i]]]=i;
--a[len[i]];
}
for(int i=t;i;--i)merge(rt[fail[b[i]]],rt[b[i]]);
}
}
namespace B{
int to[1000005][26],fail[1000005],len[1000005],now,t,l;
void init(){
now=t=1;l=0;
for(int i=0;i<26;++i)to[1][i]=0;
fail[1]=0;
}
int extend(int x,int mx){
int nw=++t,i=now;
fail[nw]=0;
for(int j=0;j<26;++j)to[nw][j]=0;
len[nw]=++l;
for(;i&&!to[i][x];i=fail[i])to[i][x]=nw;
if(!i)fail[nw]=1;
else if(len[i]+1==len[to[i][x]])fail[nw]=to[i][x];
else{
int o=to[i][x],n=++t;
len[n]=len[i]+1;
fail[n]=fail[o];
for(int j=0;j<26;++j)to[n][j]=to[o][j];
fail[o]=fail[nw]=n;
for(;i&&to[i][x]==o;i=fail[i])to[i][x]=n;
}
now=nw;
return len[nw]-max(len[fail[nw]],mx);
}
}

int main(){
freopen("name.in","r",stdin);
freopen("name.out","w",stdout);
read(s);
n=strlen(s);
for(char *i=s;*i;++i)A::extend(*i-'a');
A::work();
int q,l,r;
read(q);
for(;q;--q){
read(t);
read(l,r);
int at=1,len=0;
ll ans=0;
B::init();
for(char *i=t;*i;++i){
for(;at!=1 && !A::to[at][*i-'a'];at=A::fail[at]){
len=A::len[A::fail[at]];
}
if(A::to[at][*i-'a']){++len;at=A::to[at][*i-'a'];}
while(len&&!query(rt[at],l+len-1,r)){
--len;
if(A::len[A::fail[at]]==len)at=A::fail[at];
}
ans+=B::extend(*i-'a',len);
}
write(ans,'\n');
}
return 0;
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×