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的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...
随机推荐
- Led Candle Light - Safe, Cost-Effective, Versatile, Realistic Flame Lighting
Candles have been used to remove light for centuries, but it took hundreds of years to make better c ...
- MyBatis知识点整理
1.MyBatis一般使用步骤 1.1获取Configuration实例或编写配置文件 //获取Configuration实例的样例 TransactionFactory transactionFac ...
- django项目日志
接口中,写一条日志: 日志工具文件如下:
- 网页链接在QQ内因多人投诉被拦截的解决方案
背景 相信大家经常会遇到一个头疼的问题就是,明明自己的网页没有违规内容(比如线下活动的推广),但链接在QQ内转发分享会被QQ管家拦截,导致用户无法访问. 那么当大家遇到这个问题的时候应该怎么办呢?不用 ...
- 微信小程序UDP通信,注意点 接收 onMessage 收到的message是ArrayBuffer缓冲,不能直接输出,要另转String处理
1.WXML 页面代码 <!--index.wxml--> <view class="container"> <view class="us ...
- 使用Quartz Job 简单的做一个定时服务
第一步:创建一个windows服务 第二步:通过NuGet 安装Quartz (我搜索了Quartz 关键字 安装了 ) 第三步 代码部分 任务类 如 多个任务 就多几个类 public class ...
- 【音乐欣赏】《Happy Together (The Turtles Cover)》 - Unlike Pluto / Why Mona
曲名:Happy Together 作者:Unlike Pluto.Why Mona [00:11.746] Imagine me and you, I do [00:14.478] I think ...
- bit Buffer
在音频流解析过程中,常常会涉及到顺序读取某些bit的操作. #include<stdio.h> typedef struct _BIT_BUF { unsigned char buffer ...
- 多租户SaaS的数据库设计模式
前言 在设计多租户SaaS应用程序时,您必须仔细选择最适合您应用程序需求的租户模型.租户模型确定每个租户的数据如何映射到存储.您选择的租户模式会影响应用程序设计和管理.以后切换到另一个模型有时代价昂贵 ...
- markdown整理
html标签# h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 一级标题:内容=== 二级标题:内容--- 强调文字:>内容 链接:[文字](链接地址) 图 ...