菜鸡求助
查看原帖
菜鸡求助
219814
xiezihan楼主2020/7/19 13:36
#include<bits/stdc++.h>
using namespace std;
struct xxx
{
	string s;
	int deep;
};
string A[10],B[10];
int cnt=0;
string a,b;
map<string,int>m;
int flag;
string check(string s,int i,int j)
{
	int n=s.length();
	int l=A[j].length();
	if(i+l>n)
	{
		return "";
	}
	string tmp;
	tmp=s.substr(i,l);
	if(tmp==A[j])
	{
		s.replace(i,l,B[j]);
		return s;
	}
	else
	{
		return "";
	}
}
void bfs()
{
	m.clear();
	m[a]=0;
	queue<xxx>q;
	q.push((xxx){a,0});
	while(!q.empty())
	{
		xxx now;
		now=q.front(); 
		q.pop();
		if(now.deep>10)
		{
			return ;
		}
		if(now.s==b)
		{
			return ;
		}
		string n;
		for(int i=0;i<cnt;i++)
		{
			for(int j=0;j<cnt;j++)
			{
				n=check(now.s,i,j);
				if(n!="")
				{
					if(m.count(n)==0)
					{
						m[n]=m[now.s]+1;
						q.push((xxx){n,now.deep+1});
					}
				}
			} 
		}	
	}
}
int main()
{
	cin>>a>>b;
	while(cin>>a[cnt]>>b[cnt])cnt++;
	bfs();
	if(m.count(b)==0)
	{
		printf("NO ANSWER!");
	} 
	else
	{
		printf("%d",m[b]);
	}
    return 0;
}

输出NO ANSWER!

2020/7/19 13:36
加载中...