这道题硬是让我用STL水过.......而且题解里说的什么双向宽搜,交替扩展...............
这道题反正,STL用就用吧,但是状态数可以卡到千亿级别,因为这个东西是阶乘扩展的,然后我们发现他的深度会极大地影响状态数,然而如果我们把深度缩小为0.5倍,那么他的状态数也就是百万级别的,所以我们可以多源搜索来进行深度优化。
由此可见多源搜索是一个方式,深度优化是一种十分有效的优化.
#include <map>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
namespace Hash{
const int P=;
const int K=;
bool hash[];
inline int Hash(std::string s){
int len=s.size(),ret=;
for(int i=;i<len;i++)
ret=(ret*K%P+s[i])%P;
return ret;
}
inline bool ex(std::string s){
return hash[Hash(s)];
}
}
using namespace std;
vector<string> temp,now;
string a,b,stage[][];
int t;
map<string,bool> just;
int main(){
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
cin>>a>>b;
while(cin>>stage[++t][])
cin>>stage[t][];
t--;
now.push_back(a);
for(int k=;k<=;k++){
temp.clear();
for(int i=;i<now.size();i++)
for(int j=;j<=t;j++)
if(now[i].find(stage[j][],)!=string::npos){
int pos=;
while(now[i].find(stage[j][],pos)!=string::npos){
string s=now[i];
pos=now[i].find(stage[j][],pos);
s=s.replace(pos,stage[j][].size(),stage[j][]);
if(Hash::ex(s)==){
Hash::hash[Hash::Hash(s)]=;
if(s==b){
printf("%d",k);
return ;
}
temp.push_back(s);
}
pos+=stage[j][].size();
}
}
now=temp;
}
printf("NO ANSWER!");
return ;
}