社区讨论

求调悬关

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

讨论操作

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

当前回复
0 条
当前快照
1 份
快照标识符
@lukjumj2
此快照首次捕获于
2024/04/04 09:19
2 年前
此快照最后确认于
2024/04/04 12:38
2 年前
查看原帖

代码可以正确的让子串变化,可是为什么次数没有+1呢

献上我可爱的Code:
CPP
#include <bits/stdc++.h>
using namespace std;
string a,b,a1[7],b1[7];
int n=1;
struct node
{
	string x;
	int y;
};
queue<node> q;
void bfs()
{
	q.push({a,0});
	while(!q.empty())
	{
		node h=q.front();
		if(h.y>10)
		{
			cout<<"NO ANSWER!";
			return ;
		}
		for(int i=1;i<=n;i++)
		{
			int p=h.x.find(a1[i],0);
			while(true)
			{
				if(p==-1)
					break;
				string c=h.x.replace(p,a1[i].size(),b1[i]);
				q.push({c,h.y+1});
				cout<<h.x<<" "<<h.y<<endl;
				if(c==b)
				{
					cout<<h.y+1;
					return ;
				}
				p=h.x.find(a1[i],p+1);
			}
		}
		q.pop();
	}
}
int main()
{
	cin>>a>>b;
	while(cin>>a1[n]>>b1[n])
		n++;
	n--;
	bfs();
	return 0;
}

调好给(第一个调好的)关注

回复

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

正在加载回复...