细节问题各种虐!!

其实就是简单的一个深搜

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

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

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. python笔记-列表和元组

    列表和元组: -可以将列表和元组当成普通的数组 -列表和元组可以保存任意类型的python对象 -通过从0开始的数字索引访问元素 -列表和元组可以存储不同类型的对象 列表和元组的区别: -列表元素使用 ...

  2. js与php中一些相似函数的对比

    一:substr js中:stringObject.substr(start,length)   一个中文算一个字符,一个英文也算一个字符 <script type="text/jav ...

  3. atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4

    atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4 目前近况:: 更换数据库,但是是使用spring集成的. <!-- hibernate配置文件路径 --> ...

  4. vim -d file01 file02 diff file01 file02 对比两文件的不同

    [root@86 vhosts]# vim -d defaul.conf.bak zabbix.xinxianm.com.conf server { | server { listen 80; | l ...

  5. 线程相关函数(2)-pthread_self()获取调用线程ID

    获取调用线程tid #include <pthread.h>pthread_t pthread_self(void); 示例: #include <pthread.h> #in ...

  6. 怎么使用Less/Sass编译工具koala

    怎么使用Less/Sass编译工具koala 如何使用Less/Sass编译工具koala 一.SASS调试插件的方法 如需调试功能,请在编译输出的时候输出debug信息,那样解析的css文件中就会包 ...

  7. Qt学习过程中遇到的问题

    由于工作需要,开始使用Qt,由于在网上找的教程文档时针对qt3的,所以在学习的过程遇到了许多由于版本不一致造成的问题,因此记录下来. 参考的文档是:Qt入门教程 详细讲解版 本机Qt版本为:Qt5.3 ...

  8. libcpmt.lib (xxx.obj) LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in XXX.obj

    问题描述: 这样的,我写了个NString类,然后用的VS2013的命令行编译的(NMAKE.exe),并用LNK.exe打包成了NString.lib 然后后来我在VS2013里面建了一个proje ...

  9. 网页尺寸offsetHeight,offsetWidth

    网页尺寸offsetHeight offsetHeight和offsetWidth,获取网页内容高度和宽度(包括滚动条等边线,会随窗口的显示大小改变). 一.值 offsetHeight = clie ...

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

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