trie 50pts 玄关求调
查看原帖
trie 50pts 玄关求调
754639
I_Was_Spasmodic楼主2024/7/29 21:14

rt,目标是原版数据,因为刚学 trie 还没学 ACAM芝士提交记录

对着题解的 trie 看了一下,感觉写的差不多,但是就是调不出来哪挂了(

代码(可能很丑):

#include<bits/stdc++.h>
using namespace std;
const int N=25;
const int M=2e6+5;
struct nt{
	int p[26];
	bool isword=0;
}t[205];
char word[N],sen[M];
bool dp[M];			// dp[i] :以 i 结尾,能否找出一组匹配 
int top,n,m,len;
void ins()
{
	int now=1;
	for(int i=1;i<=strlen(word+1);i++)
	{
		if(t[now].p[word[i]-'a']==0)t[now].p[word[i]-'a']=++top;
//		printf("t[%d].p[%d]= %d \n",now,word[i]-'a',t[now].p[word[i]-'a']);
		now=t[now].p[word[i]-'a'];
	}
	t[now].isword=1;
}
void ask(int k)		// k 是上次结束匹配的位置,这次从 k + 1 开始匹配 
{
	int now=1;
	for(int i=1;i<=10 and i+k<=len;i++)
	{
		if(t[now].p[sen[k+i]-'a']==0)return;
		now=t[now].p[sen[k+i]-'a'];
		if(t[now].isword)
		{
			dp[k+i]=1;		// 能够达到 
//			printf("find word end at %d\n",k+i);
		}
	}
}
int main()
{
	cin.tie(0);
	cin>>n>>m;
	while(n--)
	{
		cin>>word+1;
		ins();
	}
	while(m--)
	{
		cin>>sen+1;
		len=strlen(sen+1);
		int maxn=0;
		dp[0]=1;
		fill(dp+1,dp+len+1,0);
		for(int i=0;i<len;i++)
		{
			if(dp[i])
			{
//				printf("to %d :\n",i);
				maxn=i;
				ask(i);				// 从 i + 1 为起点,查找匹配 
			}
		}
		if(dp[len])maxn=len;
		printf("%d\n",maxn);
	}
}
2024/7/29 21:14
加载中...