ZOJ1004 Anagrams by Stack
题目大意:规定 i 为入栈,o 为出栈,现在给两个字符串st1,st2,现在要将st1转化为st2,转化方法是,st1中字符从头开始入栈,并合理出栈构造出st2。请输出所有可能的出入栈步骤。
深度优先搜索+回溯~
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int len;
stack<char> st;
vector<char> path;
void dfs (int ipush,int ipop) {
if (ipush==len&&ipop==len) {
for (int i=;i<path.size();i++)
printf ("%c ",path[i]);
printf ("\n");
return;
}
if (ipush+<=len) {
st.push(s1[ipush]);
path.push_back('i');
dfs(ipush+,ipop);
st.pop();
path.pop_back();
}
if (ipop+<=len&&!st.empty()&&st.top()==s2[ipop]) {
char now=st.top();
st.pop();
path.push_back('o');
dfs(ipush,ipop+);
st.push(now);
path.pop_back();
}
}
int main () {
while (cin>>s1>>s2) {
len=s1.length();
printf ("[\n");
dfs(,);
printf ("]\n");
}
return ;
}
ZOJ1004 Anagrams by Stack的更多相关文章
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- HDU ACM 1515 Anagrams by Stack
Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- ZOJ 1004 Anagrams by Stack(DFS+数据结构)
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...
- 【Acm】算法之美—Anagrams by Stack
题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...
- Anagrams by Stack(深度优先搜索)
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- [ZJU 1004] Anagrams by Stack
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- ZOJ 1004 Anagrams by Stack
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...
- ZOJ Anagrams by Stack(堆栈中的搜索)
个人心得:算法书中的第一个例题就来了一个下马威,虽然题意很好理解但是做起来确实这么不顺手,所以自己对于搜索和堆栈理解的并不是很好, 以前也是很多这样的题目无法实施,这题要做的很明确就是输出正确的能依靠 ...
- hdu 1515 Anagrams by Stack
题解: 第一:两个字符不相等(即栈顶字符与目标字符不相等):这种情况很容易处理,将匹配word的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...
随机推荐
- 题解【CJOJ1071/UVA】硬币问题
P1071 - [Uva]硬币问题 Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为S?输出硬币数目 ...
- EAC3 Spectral Extension Process
1.overview 当使用Spectral extension时,channel中的高频部分的transform coefficients由低频部分合成. transform coefficient ...
- 【Python】蟒蛇绘制
来画一只你的小蛇吧! 1. 2. 3.了解turtle库 Turtle,也叫海龟渲染器,使用Turtle库画图也叫海龟作图.Turtle库是Python语言中一个很流行的绘制图像的函数库.海龟渲染器, ...
- C/S编程
https://blog.csdn.net/antony1776/article/details/73717666 实现C/S程序,加上登录注册聊天等功能. 然后要做个协议的样子出来. 比如说注册功能 ...
- 闲来无事.gif
- mysql开启远程访问及相关权限控制
开启mysql远程访问: 授予用户user 密码 passwd 所有权限 所有主机IP可访问 授权语句:Grant <权限> on 表名[(列名)] to 用户 With grant op ...
- .Net Core 2.0 App中读取appsettings.json
引用: Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.FileExtensionsMicrosoft.Ext ...
- Centos6.10-FastDFS-Storage.conf配置示例
Centos610系列配置 # is this config file disabled # false for enabled # true for disabled disabled = fals ...
- 深入delphi编程理解之消息(五)重写(override)dispatch、wndproc方法和Application.OnMessage事件
dispatch.wndproc是VCL framework在TWinCtronl定义的虚拟方法,下面程序通过重写(override)这两函数拦截WM_LBUTTONDOWN消息,来与Applicat ...
- inconsistent use of tabs and spaces in indentation
这个报错就是混用了tab和4个空格造成的,检查代码,要不全部用tab,要不全部用4个空格,或者用idle编辑器校正