stack+DFS ZOJ 1004 Anagrams by Stack
/*
stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的
然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f; string s1, s2;
int len;
stack<char> S;
vector<char> V; void DFS(int push, int pop)
{
if (push == len && pop == len)
{
for (int i=; i<V.size (); ++i)
cout << V[i] << " ";
cout << endl;
} if (push + <= len) //入栈
{
S.push (s1[push]);
V.push_back ('i');
DFS (push+, pop);
S.pop ();
V.pop_back ();
} if (pop + <= push && pop + <= len && S.top () == s2[pop]) //出栈
{
char ch = S.top ();
S.pop ();
V.push_back ('o');
DFS (push, pop+);
S.push (ch);
V.pop_back ();
}
} int main(void) //ZOJ 1004 Anagrams by Stack
{
//freopen ("ZOJ_1004.in", "r", stdin); while (cin >> s1 >> s2)
{
V.clear ();
while (!S.empty ()) S.pop (); len = s1.length (); cout << "[" << endl;
DFS (, );
cout << "]" << endl;
} return ;
} /*
[
i i i i o o o i o o
i i i i o o o o i o
i i o i o i o i o o
i i o i o i o o i o
]
[
i o i i i o o i i o o o
i o i i i o o o i o i o
i o i o i o i i i o o o
i o i o i o i o i o i o
]
[
]
[
i i o i o i o o
]
*/
stack+DFS ZOJ 1004 Anagrams by Stack的更多相关文章
- ZOJ 1004 Anagrams by Stack(DFS+数据结构)
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...
- ZOJ 1004 Anagrams by Stack
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...
- [ZOJ 1004] Anagrams by Stack (简单搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求 ...
- [ZJU 1004] Anagrams by Stack
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- 1004 Anagrams by Stack
考察DFS的应用,用栈描述字符串的变化过程. #include <stdio.h> #include <string.h> int len1,len2; ],str2[],st ...
- Anagrams by Stack(深度优先搜索)
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- 【Acm】算法之美—Anagrams by Stack
题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...
- 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写三个参数:depth搜索深 ...
随机推荐
- Android软键盘隐藏,遮挡EidtText解决办法
一.自动弹出软键盘 Timer timer=new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodM ...
- HLG2062(make,heap问题)
最小的n个和 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 129(37 users) Total Accepted: 35(29 u ...
- HDOJ 2544
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- EtherCAT报文寻址
EtherCAT通信通过主站发送EtherCAT数据帧读写从站设备的内部存储区实现. 一个EtherCAT网段相当于一个以太网设备,主站首先通过以太网数据帧头的MAC地址寻址到网段,然后使用Ether ...
- 1.10 编程之美-双线程下载[double threads to download]
[本文链接] http://www.cnblogs.com/hellogiser/p/double-threads-to-download-and-write.html [题目] 网络上下载数据,然后 ...
- 将Excel文件.xls导入SQL Server 2005
SQL2005 Microsoft SQL Server Management Studio Express管理器里,右键单击一个数据库,指向“任务”,再单击“导入数据”或“导出数据”中没有这个选项, ...
- linux网络编程_1
本文属于转载,稍有改动,以利于学习. (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个 ...
- cocos2dx实现经典飞机大战
游戏开始层 #ifndef __LayerGameStart_H__ #define __LayerGameStart_H__ #include "cocos2d.h" USING ...
- 一、HTML和CSS基础--网页布局--实践--导航条菜单的制作
案例一:导航菜单的制作 垂直菜单
- linux shell的切换
查看系统可用shell种类:(一般是bash shell) ➜ ~ chsh -l /bin/sh /bin/bash /sbin/nologin /bin/dash /bin/zsh 修改当前的sh ...