trie树魔板

版权声明:转载记得声明噢! https://blog.csdn.net/Richard__Luan/article/details/82806787

来补个坑,复习复习

#include<cstdio>
#include<iostream>
using namespace std;
int n;
int son[100005][35];
bool pd[100005];
int edge=0;
int ans;
int main()
{
	cin>>n;
	string s;
	for(int i=1;i<=n;i++)
	{
	//scanf("%s",&s);	
	cin>>s;	
	int now=0;	
	//cout<<s.length();
	for(int i=0;i<s.length();i++)	
	{
		if(!son[now][s[i]-'a'])
		{
			edge++;
			son[now][s[i]-'a']=edge;
		}
		now=son[now][s[i]-'a'];	
	}
	pd[now]=1;			
	} 
	while(cin>>s)
	{
		int now=0;
		int k=0;
		for(int i=0;i<s.length();i++)
		{
			now=son[now][s[i]-'a'];
			if(!now)
			{
				ans++;
				k=1;
				break;
			}
		}
		if(!pd[now]&&!k)ans++;
	}
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Richard__Luan/article/details/82806787