蒻篛求助,80分,wa5点
查看原帖
蒻篛求助,80分,wa5点
542974
William_qwq楼主2022/3/3 20:40
#include<bits/stdc++.h>
using namespace std;
multimap<string,string> a;
map<string,int> ans;
string st,nd;
struct node
{
	string str;
	int step;
};
queue<node> q;
void bfs()
{
	q.push(node{st,0});
	ans[st]=0;
	while(!q.empty())
	{
		string str=q.front().str;
		int step=q.front().step;
		q.pop();
		//cout<<str<<" "<<step<<endl;
		ans[str]=step;
		if(step>10) continue;
		if(str==nd) break;
		for(map<string,string>::iterator it=a.begin();it!=a.end();it++)
		{
			string x=it->first;
			string y=it->second;
			int p=0;
			while(str.find(x,p)!=-1)
			{
				p=str.find(x,p);
				string nstr=str.substr(0,p)+y+str.substr(p+x.size());
				if(!ans.count(nstr))
				{
					q.push(node{nstr,step+1});
				}
				p++;
			}
		}
	}
}
int main()
{
    int i,j,k;
    cin>>st>>nd;
    string x,y;
    while(cin>>x>>y)
    {
   	    a.insert({x,y}); 
    }
    bfs();
    if(ans[nd]) cout<<ans[nd];
    else cout<<"NO ANSWER!";
    return 0;
}

2022/3/3 20:40
加载中...