[CDOJ 8] God Only Knows! [AC自动机]

2 篇文章 0 订阅
2 篇文章 0 订阅

给一个大串和若干个小串,问大串有多少个子串不包含任何小串。只要两个子串是不同位置的就认为是不同的子串。

AC自动机。用所有的子串减去不符合要求的子串。

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int K=26;
const int N=100010;
struct Node {
	Node *ch[K],*fail;
	int match,len,h;
	Node () {
		memset(this,0,sizeof(Node));
	}
	void *operator new (size_t,void *p) {
		return p;
	}
};
Node *que[N];
Node nodes[N],*root,*superRoot,*cur;
void clear() {
	cur=nodes;
	superRoot=new(cur++)Node();
	root=new(cur++)Node();
	root->h=0;
	root->fail=superRoot;
	for (int i=0;i<K;i++) superRoot->ch[i]=root;
	superRoot->match=-1;
}
void insert(char *s) {
	Node *t=root;
	for (;*s;s++) {
		int x=*s-'a';
		if (t->ch[x]==NULL) {
			t->ch[x]=new(cur++)Node();
			t->ch[x]->h=t->h+1;
		}
		t=t->ch[x];
	}
	t->match++;
}
void build() {
	int p=0,q=0;
	que[q++]=root;
	superRoot->len=-1;
	while (p!=q) {
		Node *t=que[p++];
		if (t->fail->len==-1&&t->match!=0) t->len=t->h;
		else t->len=t->fail->len;
		for (int i=0;i<K;i++) {
			if (t->ch[i]) {
				t->ch[i]->fail=t->fail->ch[i];
				que[q++]=t->ch[i];
			} else t->ch[i]=t->fail->ch[i];
		}
	}
}

char s[1000001];
char s2[100001];
int ls,n,last;
long long ans;

int main() {
	int t,tt,i;
	scanf("%d",&t);
	for (tt=1;tt<=t;tt++) {
		clear();
		scanf("%s",s);
		ls=strlen(s);
		scanf("%d",&n);
		for (i=0;i<n;i++) {
			scanf("%s",s2);
			insert(s2);
		}
		ans=(long long)ls*(ls+1)/2;
		build();
		Node *t=root;
		last=0;
		for (i=0;i<ls;i++) {
			t=t->ch[s[i]-'a'];
			if (t->len!=-1) {
				last=max(last,i-t->len+2);
			}
			ans-=last;
		}
		printf("Case #%d: %lld\n",tt,ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值