深搜———ZOJ 1004:anagrams by stack
细节问题各种虐!!
其实就是简单的一个深搜
看成二叉树来理解:每个节点有两个枝:入栈和出栈。
剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈
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的更多相关文章
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- ZOJ 1004 Anagrams by Stack
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...
- ZOJ 1004 Anagrams by Stack(DFS+数据结构)
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...
- [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 ...
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...
- 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 ...
随机推荐
- ffmpeg command
1. 列出当前系统的设备列表 ffmpeg -list_devices true -f dshow -i dummy 2. 列出设备Integrated Camera的信息 ffmpeg -list_ ...
- HDU 4946 Area of Mushroom 共线凸包
题意是在二维平面上 给定n个人 每一个人的坐标和移动速度v 若对于某个点,仅仅有 x 能最先到达(即没有人能比x先到这个点或者同一时候到这个点) 则这个点称作被x占有 若有人能占有无穷大的面积 则输出 ...
- 趣味SQL——创建指定的数据类型
原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46908843 趣味 ...
- Memory leak patterns in JavaScript
Handling circular references in JavaScript applications Plugging memory leaks in JavaScript is easy ...
- windows gvim插入当前时间
:nnoremap <F5> "=strftime("%c")<CR>P :inoremap <F5> <C-R>=str ...
- vim 创建文件自动生成头部注释
知识点: 1. autocmd命令: 当读写一个文件时,自动执行指定的命令; 2. autocmd event: BufNewFile 当文件不存在时开始写文件; 3. exec命令 execute命 ...
- Windows Phone实用教程:利用Blend为程序添加设计时数据
[前言] Blend自诞生那一天起就伴随这开发者如此的评价: 有VS还用Blend干啥,直接码代码就好了. Blend会生成一堆垃圾无用代码,很不爽. 对于这类我只会在心里评价,当你并不真正了解一样事 ...
- Project Euler:Problem 34 Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...
- 0810 smarty
1.Smarty简介(why,what) why:smarrty 将前端工程师和程序员的工作分开,让前端工程师完成前台页面的工作,程序员完成后台的业务逻辑.what模版引擎是用来将PHP代码和模版页组 ...
- 常见的装包的三种宝,包 bao-devel bao-utils bao-agent ,包 开发包 工具包 客户端
常见的装包的三种宝,包 bao-devel bao-utils bao-agent ,包 开发包 工具包 客户端