为什么会出现Ctrl、z之后这个代码才能运行的问题
查看原帖
为什么会出现Ctrl、z之后这个代码才能运行的问题
1162380
liverxiwo楼主2023/11/2 13:46

代码如下

#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
map <string,bool> ed;
map <string,int> st;
string a,b;
int t=1;
string change[30][3];
int ans=888;
void dfs(string now,int step){
	if(step>10)return;
	if(now==b){
		ans=min(ans,step);
		return;
	}
	if(ed[now]){
		if(step>=st[now])return;
	}
	ed[now]=1;st[now]=step;
	int loc=0;string changed;
	for(int i=1;i<=t;++i){
		loc=-1;
		 while(1){
		 	loc=now.find(change[i][0],loc+1);
		 	if(loc==-1)break;
		 	changed=now;
		 	changed.erase(loc,change[i][0].size());
		 	changed.insert(loc,change[i][1]);
		 	dfs(changed,step+1);
		 }
	}
	return;
}
int main(){
	cin>>a>>b;
		while(cin>>change[t][0]>>change[t][1]){
		t++;
	}
	t--;
	dfs(a,0);
	if(ans==888){
		cout<<"NO ANSWER!"<<endl;
		return 0;
	}
	cout<<ans<<endl;
	return 0;
} 

这个代码借鉴了@Time_Rune 的题解优化后代码,但是没有深度迭代优化,所以五个能过四个,本人开启和关闭o2都试过,和优化无关。 另外@Time_Rune 大佬的优化代码存在严重未查明问题,本人在电脑上试运行过不了样例。。。

2023/11/2 13:46
加载中...