题目大意就是给定两个char类型的组合方式,问怎么组合转化可以让原始串转变成目标串(一个char型字符),并且输出中间转化过程。

整体思路:就是纯暴力。中间一定要记忆化一下。

There is a special multiplication operator such that
Table of special multiplication operation
Thus
ab
=
b
,
ba
=
c
,
bc
=
a
,
cb
=
c
, . . .
For example, you are given the string
bbbba
and the character
a
,
(b(bb))(ba) = (bb)(ba)  [as bb = b]
= b(ba)     [as bb = b]
= bc
[as ba = c]
= a
[as bc = a]
By adding suitable brackets,
bbbba
can produce
a
according to the above multiplication table.
You are asked to
write  a  program
to show the morphing steps of a string into an expected
character, or otherwise, output `
None exist!
' if the given string cannot be morphed as expected.
Input
The  rst line of the input  le gives the number of test cases. Each case consists of two lines. The  rst
line is the starting string which has at most 100 characters. The second line is the target character.
All characters in the input are within the range of
a
-
c
.
Output
For each test case, your output should consist several lines, showing the morphing steps of a string into
the character. In case there are more than one solution, always try to start the morphing from the left.
Print a blank line between consecutive sets of output.
Sample Input
2
bbbba
a
bbbba
a
Sample Output
bbbba
bbba
bba
bc
a
bbbba
bbba
bba
bc
a

代码:

#include<bits/stdc++.h>
using namespace std;
int t;
string h1,h2;
map<string ,string >haha;
char change(char a,char b)
{if(a=='a'){if(b=='a'||b=='b')return 'b';else if(b=='c')return 'a';}else if(a=='b'){if(b=='a')return 'c';else if(b=='b')return 'b';else if(b=='c')return 'a';}else if(a=='c'){if(b=='a')return 'a';else if(b=='b'||b=='c')return 'c';}
}int dfs(string h)
{if(h.length()==1){return h==h2;}string xpp="";for(int i=1;i<h.length();i++){xpp="";for(int j=0;j<i-1;j++){xpp=xpp+h[j];}xpp=xpp+change(h[i-1],h[i]);for(int j=i+1;j<h.length();j++){xpp=xpp+h[j];}if(haha.count(xpp)!=0)///这种状态已经找过了continue;haha[xpp]=h;///表明xpp的上一个状态是h// cout<<"^^&^"<<endl;if(dfs(xpp)==1)return 1;}return 0;
}
void digui(string h)
{if(h==h1){cout<<h1<<endl;return ;}digui(haha[h]);cout<<h<<endl;
}
int main()
{scanf("%d",&t);while(t--){cin>>h1>>h2;haha.clear();int L=dfs(h1);if(L==1){digui(h2);}else{cout<<"None exist!"<<endl;}if(t!=0)cout<<endl;}return 0;
}

string morphing 巧妙dp+dfs相关推荐

  1. POJ-3590 The shuffle Problem 置换+DP | DFS

    题目链接:http://poj.org/problem?id=3590 自己暴力给水过去了,不过效率有点低.题目要求的就是给一个数n,要你求出一种方案,一些和为n的数的最小公倍数最大.题目数据量不大, ...

  2. 洛谷 P1164:小A点菜(DP/DFS)

    题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...

  3. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  4. HDU 3336 Count the string(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给你一个字符串,计算其所有前缀在该字符串出现的次数的总和. 思路:next[j]=i,代表 ...

  5. UVa11022 String Factoring(kmp+dp)

    用dp(i,j)表示子串s[i..j]可以表示的最小长度.动态转移方程有 dp(i,j) = min{dp(i,k) + dp(k+1,j)},其中k属于[i,j].s[i..j]是周期串,最小周期为 ...

  6. NYOJ 1067 Compress String(区间dp)

    Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 One day,a beautiful girl ask LYH to help her ...

  7. HDU5863 cjj's string game(DP + 矩阵快速幂)

    题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...

  8. POJ_1088 滑雪(记忆型DP+DFS)

    Description Michael喜欢滑雪,这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  9. [CF1107E]Vasya and Binary String【区间DP】

    题目描述 Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of l ...

最新文章

  1. django用户认证系统——注册3
  2. C shell命令行解释器
  3. 结构型模式:外观模式
  4. ConcurrentHashMap能完全替代HashTable吗?
  5. linux环境特性的文件夹,在Linux环境下如何消减可执行文件或者动态库的大小
  6. 从概念到落地,中台可以解决哪些问题?怎么做?(附PPT)
  7. ES6学习笔记04:Set与Map
  8. Aligned公司在凤凰城建设数据中心将采用微电网的电力
  9. ObjectAnimator实现菜单的弹出(扇形)
  10. sybase 连接mysql_安装sybase服务器并连接数据库
  11. 台电平板(X80HD)刷WIN10
  12. springboot的定时任务注解
  13. 交叉编译openssl
  14. 公章,加盖公章,英语怎么说?
  15. 美柚:女性移动APP安全攻防战
  16. 活着,要有温暖的感觉
  17. Mysql许久不用后服务无法打开,暴力解决法(报错:服务没有报任何错误)
  18. Laravel 5.5 Artisan 命令
  19. bat批处理开发-wifi联网系列(3):查询当前连接的wifi SSID和密码,封装为bat函数(如何传递入参和返回出参)
  20. aix查看lv_(摘抄)AIX下减小lv size

热门文章

  1. 【转】关于医疗术语 CT,MR,DR,CR,DSA 等
  2. [计算机数值分析]复化梯形的递推公式的变步长算法求积分
  3. 微信小程序上传图片及文件(上传、下载、删除及预览)
  4. 推荐几本与领导力相关的书,这些书可以帮助你培养领导力
  5. Docker学习笔记2——Docker组件(幕布笔记)
  6. 电赛滚球控制系统树莓派代码
  7. 【模式识别与人工智能】【实验报告合集】Bayes + Fisher + PCA + Decision Tree + KNN + K-Means + SVM
  8. Java 学习笔记:第一章 Java入门
  9. 建立您的创业公司:提高安全性
  10. C#使用Graphics简单横向拼接图片