后缀自动机模板题hdu_HDU3695(AC自动机模板题)

/*gyt

Live up to every day*/#include#include#include#include#include#include#include`

#include#include#include#include#include

#define PI acos(-1)

using namespacestd;

typedeflong longll;

typedefdoubledb;const int maxn = 6000005;const ll maxm =1e7;const ll mod = 1e9 + 7;const int INF = 0x3f3f3f;const ll inf = 1e15 + 5;const db eps = 1e-9;const int kind=26;structnode{

node*fail;

node*next[kind];intcoun;voidnodee() {

fail=NULL;

coun=0;for (int i=0; i

next[i]=NULL;

}

}*root;charstr[maxn];chartmp[maxn];int ans=0;voidupdata() {

node*p=root;int len=strlen(str);for (int i=0; inext[pos]==NULL) {

p->next[pos]=newnode;

p->next[pos]->nodee();

p=p->next[pos];

}else p=p->next[pos];

}

p->coun++;

}voidgetfail() {

node*p=root, *son, *tmp;

queueque;

que.push(p);while(!que.empty()) {

tmp=que.front();

que.pop();for (int i=0; i<26; i++) {

son=tmp->next[i];if (son!=NULL) {if (tmp==root) {

son->fail=root;

}else{

p=tmp->fail;while(p) {if (p->next[i]) {

son->fail=p->next[i];break;

}

p=p->fail;

}if (!p) son->fail=root;

}

que.push(son);

}

}

}

}voidquery() {int len=strlen(str);

node*p, *tmp;

p=root;int cnt=0;for (int i=0; inext[pos]&& p!=root) p=p->fail;

p=p->next[pos];if (!p) p=root;

tmp=p;while(tmp!=root) {if (tmp->coun>=0) {

cnt+=tmp->coun;

tmp->coun=-1;

}else break;

tmp=tmp->fail;

}

}

ans+=cnt;

}voidsolve() {

ans=0;

root=newnode;

root->nodee();

root->fail=NULL;int n; scanf("%d", &n);

getchar();for (int i=0; i

gets(str); updata();

}

getfail();

gets(str);int len=strlen(str);int nn=0; charc;int now=0; int cnt=0;while(1) {if (now>=len) break;if (str[now]=='[') {

now++;while(str[now]>='0'&&str[now]<='9') {

nn= nn*10+(str[now]-'0');

now++;

}

c=str[now];for (int i=0; i

tmp[cnt++]=c;

nn=0;

now++;

now++;

}else{//if (str[now]==']') {//now++; continue;//}

tmp[cnt++]=str[now]; now++;

}//cout<

}

tmp[cnt]='\0';for (int i=0; i<=cnt; i++) str[i]=tmp[i];//cout<

query();

reverse(str, str+strlen(str));//cout<

query();

cout<

}intmain() {int t = 1;//freopen("in.txt", "r", stdin);

scanf("%d", &t);while(t--)

solve();return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值