细节问题各种虐!!

其实就是简单的一个深搜

看成二叉树来理解:每个节点有两个枝:入栈和出栈。

剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈

dfs写三个参数:depth搜索深度,npush压栈数,npop出栈数

npush用于记录压栈数:主要判断当前压栈是否合理,以及要压入的元素在原串种的位置

npop用于记录出栈数:判断生成的目标串元素的位置

当npush==npop==目标串时,说明生成了一个可执行的操作串

注意输出操作串的时候一定要用depth参数来控制,因为在多次输入时string 的最大长度已经改变,只有利用depth才能确定该字符串的那一部分是当前所生成的。

贴代码!

# include<iostream>
# include<string>
# include<cstdio>
# include<cstring>
using namespace std; string source;
string target; char solution[];
char s[]; int top; void dfs(int depth, int npush, int npop)
{
if (npush == target.length() && npop == target.length())
{
for (int i = ; i < depth; i++)
{
cout << solution[i]<<" ";
}
cout << endl;
return;
} if (npush < target.length())
{
s[top++] = source[npush];
solution[depth] = 'i';
dfs(depth + , npush + , npop);
top--;
} if (top > && s[top - ] == target[npop])
{
solution[depth] = 'o';
char temp = s[top - ];
top--;
dfs(depth + , npush, npop + );
s[top++] = temp;
} return; } int main()
{
while (cin>>source>>target)
{
top = ;
cout << "[" << endl;
if (source.length() == target.length())
dfs(,,);
cout << "]" << endl;
} return ;
}

深搜———ZOJ 1004:anagrams by stack的更多相关文章

  1. stack+DFS ZOJ 1004 Anagrams by Stack

    题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...

  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(DFS+数据结构)

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

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

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

  5. [ZJU 1004] Anagrams by Stack

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

  6. 1004 Anagrams by Stack

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

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

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

  8. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

  9. ZOJ(ZJU) 1002 Fire Net(深搜)

    Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...

随机推荐

  1. Atitit.编程语言的主要的种类and趋势 逻辑式语言..函数式语言...命令式语言

    Atitit.编程语言的主要的种类and趋势 逻辑式语言..函数式语言...命令式语言 1. 编程语言的主要的种类 逻辑式语言..函数式语言...命令式语言 1 2. 逻辑式语言,,不必考虑实现过程而 ...

  2. CentOS开机的时候卡在进度条一直进不去 F5(是关键)

    这看不出开机启动卡在哪里,只好重启按住"e"键,进入启动菜单: 然后移动到第二项kernel...接着按e进入编辑 去掉rhgb quiet字样 按回车保存回到选择项 按b启动它就 ...

  3. 检查是否是移动端(Mobile)、ipad、iphone、微信、QQ等

    <script type="text/javascript"> //判断访问终端 var browser={ versions:function(){ var u = ...

  4. 中兴ZXV10 B860AV1.1 全TTL操作完美破解

    本文转自:http://www.znds.com/tv-496624-1-1.html 1)前期准备工作 1.1 拆开盒子,TTL接线,这个论坛里有好多其它帖子,就不再详细描述. 1.2 复制需要安装 ...

  5. Django接受ajax传过来的数组

    $.ajax({ cache: false, type: "POST", url: "/userdelete/", traditional:true, //加上 ...

  6. jquery easy ui 验证框架

    引入参考最下面API ) var reg = /^1[3|4|5|8|9]\d{9}$/; return reg.test(value); }, message: '输入手机号码格式不准确.' } } ...

  7. 在sql中根据成绩显示学生排名

    1.准备 create table newtable ( name ), yuwen ), shuxue ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; , ); , ...

  8. openvpn 移植之buildroot添加相关选项

    openvpn 移植第一步,在buildroot 内添加 openssl ,openvpn , 另外还有一个 RSA 的支持,我不确定这个需要程度如何,但是也添加进去了. buildroot 添加相关 ...

  9. /proc/meminfo分析

    参考: 1. linux/Documentation/filesystems/proc.txt 2. Linux 中 /proc/meminfo 的含义 3. redhat deployment gu ...

  10. Scala中List(Map1,Map2,Map3 ....) 转成一个Map

    这个问题研究好久...头大,不记得有fold用法了. fold函数:折叠,提供一个输入参数作为初始值,然后大括号中应用自定义fun函数并返回值. list.fold(Map()){(x,y)=> ...