求助!WA on #3 #6 #8
查看原帖
求助!WA on #3 #6 #8
704562
GUO120822楼主2024/6/24 17:06
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,m,idx,i,f[N][28];
int c[N],k;
vector<int> a;
string s;
void ins(string s,int id)
{
	int i,p=0,x;
	for (i=0;s[i];i++)
	{
		x=s[i]-'a';
		if (!f[p][x]) f[p][x]=++idx;
		p=f[p][x];
	}
	c[p]=id;
}
bool check(string s)
{
	int i,p=0,x;
	for (i=0;s[i];i++)
	{
		x=s[i]-'a';
		if (!f[p][x]) return false;
		p=f[p][x];
	}
	if (c[p]) return true;
	return false;
}
void dfs(int x,int pos,int cnt)
{
	if (pos==s.size()) 
	{
		if (c[x]) a.emplace_back(c[x]);
		return;
	}
	//三种操作:
	//1.跳过一个字符 
	//2.乱搞一个字符,替换。 
	//3.先直接调到一个字符的点上,再继续。 
	int d=s[pos]-'a';
	int p;
	string tmp;
	char w;
	if (!f[x][d])
	{
		if (cnt==1) return;
		else
		{
			dfs(x,pos+1,cnt+1);
			for (p=0;p<=25;p++) dfs(f[x][p],pos+1,cnt+1);
			for (p=0;p<=25;p++) dfs(f[x][p],pos,cnt+1);
		}
	}
	else
	{
		if (cnt==1) dfs(f[x][d],pos+1,cnt);
		else
		{
			dfs(f[x][d],pos+1,cnt);
			dfs(x,pos+1,cnt+1);
			for (p=0;p<=25;p++) dfs(f[x][p],pos+1,cnt+1);
			for (p=0;p<=25;p++) dfs(f[x][p],pos,cnt+1);
			tmp=s;
			x=f[x][d];
			for (p=0;p<=25;p++)
			{
				if (f[x][p]) dfs(f[x][p],pos+1,cnt+1);
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for (i=1;i<=n;i++)
	{
		cin>>s;
		ins(s,i);
	}
	while (m--)
	{
		cin>>s;
		if (check(s))
		{
			cout<<-1<<"\n";
			continue;
		}
		a.clear();
		dfs(0,0,0);
		sort(a.begin(),a.end());
		k=unique(a.begin(),a.end())-a.begin();
		cout<<k<<"\n";
	}
	return 0;
}

(万分感谢)

2024/6/24 17:06
加载中...