P1032 字符变幻T了一个求助
查看原帖
P1032 字符变幻T了一个求助
245089
i_am_a_joker楼主2022/4/18 13:40
#include<bits/stdc++.h>
using namespace std;
int cnt = 1;
string stat,ed;
string a[100],b[100];
int bfs()
{
	queue<string> q;
	map<string,int> mp;
	mp[stat] = 1;
	q.push(stat);
	while(!q.empty())
	{
		string x = q.front(); q.pop();
		int lx = x.length();
		for(int i=1; i<=cnt; i++)
		{
			int ly = a[i].length();
			for(int j=lx-ly; j>=0; j--)
			{
				string y = x.substr(j,ly);
				if(y != a[i]) continue;
				string nxt = x;
				nxt.replace(j,ly,b[i]);//(begin,end,替换数组);
				mp[nxt] = mp[x]+1;
				if(mp[nxt]>11) return -1;
				if(nxt == ed) return mp[nxt]-1;
				q.push(nxt);
			}
			
		}
	}
}
int main()
{
	cin>>stat>>ed;
	while(cin>>a[cnt]>>b[cnt]) cnt++;
	cnt -= 1; cout<<cnt<<" ";
	int ans = bfs();
	if(ans != -1) cout<<ans;
	else cout<<"NO ANSWER!"; 
	return 0;
}
2022/4/18 13:40
加载中...