20分求解
查看原帖
20分求解
983593
lihaoran2012楼主2024/8/5 17:22
#include<bits/stdc++.h>
using namespace std;
string a[25],b[25];
struct node{
    string s;
    int sum;
};
queue<node>q;
int main(){
	string A,B;
	cin>>A>>B;
	int num=0;
	while (cin>>a[num]>>b[num])
		num++;
	num--;
	q.push({A,0});
	while(!q.empty()){
		node p=q.front();
		q.pop();
		for(int i=1;i<=num;i++){
			int mark=p.s.find(a[i]);
			if(mark!=-1){
				string temp=p.s;
				temp=p.s.replace(mark,a[i].size(),b[i]);
				if(temp==B){cout<<p.sum+1;return 0;}
				p.sum++;
				q.push({temp,p.sum});
			}
		}	
	}
	cout<<"NO ANSWER!";
	return 0;
}

2024/8/5 17:22
加载中...