题目传送门

 /*
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的更多相关文章

  1. ZOJ 1004 Anagrams by Stack(DFS+数据结构)

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...

  2. ZOJ 1004 Anagrams by Stack

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...

  3. [ZOJ 1004] Anagrams by Stack (简单搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求 ...

  4. [ZJU 1004] Anagrams by Stack

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  5. 1004 Anagrams by Stack

    考察DFS的应用,用栈描述字符串的变化过程. #include <stdio.h> #include <string.h> int len1,len2; ],str2[],st ...

  6. Anagrams by Stack(深度优先搜索)

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  7. 【Acm】算法之美—Anagrams by Stack

    题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...

  8. HDU ACM 1515 Anagrams by Stack

    Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. 深搜———ZOJ 1004:anagrams by stack

    细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深 ...

随机推荐

  1. firefox查看微信公众平台的数据分析时就出现不信任链接怎么办?

    昨天用360清理垃圾后火狐主页的快速拨号栏消失了,整了半天还是无法使用就重装了一下firefox,导入备份的书签,添加自己所需的附加组件,设置为隐私模式,开始继续体验.按惯例打开微信公众平台,查看数据 ...

  2. ThinkPHP 分页实现

    TP3.2框架手册,有一个数据分页,不过每次都要写太多的代码,还有中文设置等有些麻烦,做为程序开发者,有必要整理下: O.先看效果图 一.分页方法 /** * TODO 基础分页的相同代码封装,使前台 ...

  3. 当前标识(IIS APPPOOL\DefaultWebSite)没有对“C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files“的写访问权限

    将C#写的webservice发布到IIS后,通过浏览器访问测试,出现如下错误: 根据提示:对Tempory ASP.NET Files没有写访问权限,在资源管理其中定位到这个地址,发现没有这个文件夹 ...

  4. HTML快速入门4

    七.表单 1. 概述 建立交互式的站点,需要使用 HTML 表单,它可以让用户提供信息,并对此作出处理.可以建立类似 复选框.单选按钮及文本框的控件. 掌握表单的使用对 Active Server P ...

  5. 最近win7更新后出现第二次打开IDE(delphi2007)的时候提示无法打开"EditorLineEnds.ttr"这个文件

    kb2982791 - 2014年8月12日更新 - http://support.microsoft.com/kb/2982791kb2970228 - 2014年8月12日更新 - http:// ...

  6. poj2993 翻转2996

    Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2944   Accepted:  ...

  7. django inclusion_tag

    一种比较普遍的tag类型是只是渲染其它模块显示下内容,这样的类型叫做Inclusion Tag. 例如,实现以下tag: {% books_for_author author %} 渲染结果为: &l ...

  8. 什么是mixin

    转自:http://guangboo.org/2013/01/28/python-mixin-programming http://en.wikipedia.org/wiki/Mixin http:/ ...

  9. 【SpringMVC】SpringMVC系列14之SpringMVC国际化

    14.SpringMVC国际化 14.1.概述 14.2.用户切换选择语言

  10. iOS 利用Context裁剪图片

    下面的代码可以裁剪出圆形的图片, 1,先把不规则图片转成正方形图片 UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake ...