【Trie】bzoj1212 [HNOI2004]L语言

枚举每个文章里已经在Trie中被标记为可能是分割处的字符,然后再从此处跑Trie,继续向后标记。由于单词数很少,因此复杂度可以接受,O(n*m*Len)。

#include<cstdio>
#include<cstring>
using namespace std;
int n,m,L;
char s[1024*1024+100];
int ch[21*11][26];
bool is[21*11],end[1024*1024+100];
int sz;
void Insert(char s[])
{
	int U=0,len=strlen(s);
	for(int i=0;i<len;++i)
	  {
	  	int V=s[i]-'a';
	  	if(!ch[U][V]) ch[U][V]=++sz;
	  	U=ch[U][V];
	  }
	is[U]=1;
}
void work(int x)
{
	int i=x,j=ch[0][s[x]-'a'];
	while(i<L&&j)
	  {
	  	if(is[j]) end[i]=1;
	  	++i; j=ch[j][s[i]-'a'];
	  }
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) {scanf("%s",s); Insert(s);}
	for(;m;--m)
	  {
	  	scanf("%s",s); L=strlen(s); memset(end,0,L*sizeof(bool));
	  	work(0);
	  	for(int i=0;i<L-1;++i) if(end[i]) work(i+1);
	  	for(int i=L-1;i>=0;--i) if(end[i]) {printf("%d\n",i+1); goto OUT;}
	  	puts("0");
	  	OUT:;
	  }
	return 0;
}

转载于:https://www.cnblogs.com/autsky-jadek/p/4309113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值