ZOJ1004 Anagrams by Stack
2024-09-07 00:30:27
题目大意:规定 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 ;
}
最新文章
- 关于easyui datagrid 表格数据处理
- kali 更改root密码
- js动画之链式运动
- Python-10 字典
- c++ 11 sleep()
- 如何使用国内源部署Ceph?
- 用super daemon xinetd进行安全配置
- python基础整理笔记(一)
- update kernel
- gulp入门
- DB天气app冲刺第四天
- Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg
- Demo_塔防(自动生成怪物,导航,炮塔攻击,怪物掉血死忙)
- 勉強すべきURL
- zf-关于荆州首页鼠标移动到导航栏上去触发的js 显示 问题解决办法
- 消息中间件 MQ
- JavaWeb过滤器.监听器.拦截器-原理&区别-个人总结
- 关于Flume中Chanel.Selector.header解释
- Android 阴影,圆形的Button
- iOS 模拟器的“调试-位置”总是变成无的问题
热门文章
- AcWing 831. KMP字符串
- 题解【洛谷P3884】[JLOI2009]二叉树问题
- mvn + testng + allure 生成自动化测试报告
- docker启动容器报错:iptables failed
- Makefile的编写及四个特殊符号的意义@、$@、$^、$
- Palindromes _easy version 题解
- hdu 1532 Drainage Ditches(网络流)
- bistoury建库建表(一)
- 对象析构谈—— delete this 的使用及注意事项
- vue项目怎么搭建到云服务器上