直接用map暴。
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef unsigned long long LLU;
typedef long long LL;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
const int maxn = 110;
const int b[3][3]={
{1,1,0},{2,1,0},{0,2,2}
};
int a[maxn],d[maxn][maxn][3],n,tar,path[maxn][maxn];
map<string,int> dp;
map<string,string> pa;
int dfs(string s){
if(dp.count(s)) return dp[s];
if(s.length()==1&&s[0]==tar+'a'){
return dp[s] = 1;
}
dp[s] = 0;
for(int i=0;i<s.length()-1;i++){
string te;
for(int j=0;j<i;j++) te.push_back(s[j]);
te.push_back((char)(b[s[i]-'a'][s[i+1]-'a']+'a') );
for(int j=i+2;j<s.length();j++) te.push_back(s[j]);
if(dfs(te)){
dp[s] = 1;
pa[s] = te;
return 1;
}
}
return 0;
}
char s[maxn];
void print(string s){
printf("%s\n",s.c_str());
if(s.length() == 1){return ;}
print(pa[s]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
cin>>s;
string ss = s;
cin>>s; tar = s[0]-'a';
dp.clear(); pa.clear();
n = strlen(s);
if(dfs(ss)) print(ss);
else printf("None exist!\n");
if(T) printf("\n");
}
return 0;
}