有dalao帮忙调一下吗 蒟蒻#3一直无输出(万分感谢 悬关
查看原帖
有dalao帮忙调一下吗 蒟蒻#3一直无输出(万分感谢 悬关
674793
luoguhandongheng楼主2023/7/26 11:20

rt rp++ 谢谢大佬orz

#include <bits/stdc++.h>
#define int long long
using namespace std;
multimap<string,string>book;
string a,b;
map <string,bool> bookk; 
queue <pair<string,int> > q;
void bfs(){
	q.push(make_pair(a,0));
	while(!q.empty()){
		pair<string,int> h=q.front();
		if(h.second>10){
			cout<<"NO ANSWER!";
			exit(0);
		}
		if(h.first==b){	
			cout<<h.second;
			exit(0);
		}
		if(bookk[q.front().first]==1){
			q.pop();
			continue;
		}
		bookk[q.front().first]=1;
		for(auto i=book.begin();i!=book.end();i++){
			string tmp=h.first;
			string t=(i->first);
			int pos=tmp.find(t);
			while(pos!=tmp.npos){
				string ss=tmp.substr(0,pos)+(i->second)+tmp.substr(pos+t.size());
				q.push(make_pair(ss,h.second+1));
				//	cout<<ss<<endl;
				pos=tmp.find(t,pos+1);
			}
		}
		q.pop();
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin>>a>>b;
	string x,y;
	while(cin>>x>>y)
		book.insert(pair<string,string>(x,y));
	bfs();
	return 0;
}
2023/7/26 11:20
加载中...