【XSY2968】线性代数

题目来源:noi2018模拟测试赛(二十二)

毒瘤板题+提答场……真tm爽

提答求最大团,各路神仙退火神仙随机化八仙过海

题意:

题解:

支持双端插入的回文自动机板题

代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<queue>
 7 #define inf 2147483647
 8 #define eps 1e-9
 9 using namespace std;
10 typedef long long ll;
11 typedef double db;
12 int q,cnt=0,anss,top[2],last[2],son[500001][4],len[500001],fail[500001],dep[500001],st[500001];
13 ll ans=0;
14 char op[10],x[10];
15 int newn(int l){
16     len[cnt]=l;
17     return cnt++;
18 }
19 void init(){
20     memset(son,0,sizeof(son));
21     memset(st,-1,sizeof(st));
22     newn(0);
23     newn(-1);
24     top[0]=q+11;
25     top[1]=q+10;
26     last[0]=last[1]=0;
27     fail[0]=1;
28 }
29 int getfail(int u,int op){
30     while(st[top[op]]!=st[top[op]+(op?-1:1)*(len[u]+1)])u=fail[u];
31     return u;
32 }
33 void extend(int ch,int op){
34     if(op)st[++top[op]]=ch;
35     else st[--top[op]]=ch;
36     int nw=getfail(last[op],op);
37     if(!son[nw][ch]){
38         int nn=newn(len[nw]+2);
39         fail[nn]=son[getfail(fail[nw],op)][ch];
40         dep[nn]=dep[fail[nn]]+1;
41         if(len[nn]==top[1]-top[0]+1)last[op^1]=nn;
42         son[nw][ch]=nn;
43     }
44     last[op]=son[nw][ch];
45     ans+=dep[last[op]];
46 }
47 int main(){
48     scanf("%d",&q);
49     init();
50     while(q--){
51         scanf("%s%s",op,x);
52         extend(x[0]-'a',op[0]=='r');
53         printf("%lld %d\n",ans,cnt-2);
54     }
55     return 0;
56 }

猜你喜欢

转载自www.cnblogs.com/dcdcbigbig/p/10086516.html
今日推荐