社区讨论

0分求条,回答必关

P1032[NOIP 2002 提高组] 字串变换(疑似错题)参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mhj3hn1e
此快照首次捕获于
2025/11/03 20:07
4 个月前
此快照最后确认于
2025/11/03 20:07
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
string a[10],b[10];
int n;
int f(queue <string>& q,unordered_map <string,int>& da,unordered_map <string,int>& db,string a[],string b[]){
	int m=q.size();
	for(int t=0;t<m;t++){
		string s=q.front();
		q.pop();
		for(int i=0;i<s.size();i++)
			for(int j=0;j<n;j++)
				if(s.substr(i,a[j].size())==a[j]){
					string x=s.substr(0,i)+b[j]+s.substr(i+a[j].size());
					if(da.count(x))
						continue;
					if(db.count(x))
						return da[s]+1+db[x];
					da[x]=da[s]+1;
					q.push(x);
				}
	}
	return 1l;
} 
int bfs(string A,string B){
	queue <string> qa,qb;
	unordered_map <string,int> da,db;
	qa.push(A);
	da[A]=0;
	qb.push(B);
	db[B]=0;
	while(qa.size()&&qb.size()){
		int t;
		if(qa.size()<=qb.size())
			t=f(qa,da,db,a,b);
		else
			t=f(qb,db,da,b,a);
		if(t<=10)
			return t;
	} 
	return 1l;
} 
int main(){
	string x,y; 
	cin>>x>>y;
	while(cin>>a[n]>>b[n])
		n++;
	int w=bfs(x,y);
	if(w>10)
		cout<<"NO ANSWER!\n";
	else
		cout<<w<<endl;
	return 0;
} 

回复

3 条回复,欢迎继续交流。

正在加载回复...