UVA - 10981(dp ,map做内存类,仍没算出最大内存)

直接用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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值