深搜,STL,点2,5WA
查看原帖
深搜,STL,点2,5WA
639483
FiresonZ楼主2022/4/29 18:13
#include<iostream>
#include<array>
#include<string>
#include<vector>
#define reg register
using namespace std;
using gg=long long;
gg top=0;
array<string,50> from,to;
string des;
gg ans=114514;
string strturn(reg string str,const gg &p)
{
	gg len=0;
	while(str.find(from[p],len)!=string::npos)
	{
		len=str.find(from[p],len);
		str.replace(len,from[p].size(),to[p]);
		len+=to[p].size();
	}
	return str;
}
void dfs(reg string str,const gg &t)
{
	if(t>=ans)
	{
		return;
	}
	if(str==des)
	{
		ans=min(ans,t);
	}
	if(t==10)
	{
		return;
	}
	for(gg i=0;i<top;i++)
	{
        if(str.find(from[i])!=string::npos)
        {
        	dfs(strturn(str,i),t+1);
		}
	}
	return;
}
int main()
{
	string beg,s1,s2;
	cin>>beg>>des;
	while(cin>>s1>>s2)
	{
		from[top]=s1;
		to[top]=s2;
		top++;
	}
	dfs(beg,gg(0));
	if(ans==114514)
	{
		cout<<"NO ANSWER!\n";
	}
	else
	{
		cout<<ans<<'\n';
	}
	return 0;
}
2022/4/29 18:13
加载中...