[JAVA][ZOJ 1004][Anagrams by Stack]

[java] view
plain
copyprint?

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;
  3. public class Main {
  4. static String start;// record the first str
  5. static String end;// record the rearranged str
  6. public static void dfs(char[] stack, char[] sequence, int posInStart, int posInEnd, int posInStack) {
  7. if (posInStart + posInEnd == start.length() * 2) {// if all the letter has been rearranged
  8. for (int i = 0; i < sequence.length; i++) {
  9. System.out.print(sequence[i] + " ");// output the sequence
  10. }
  11. System.out.println();
  12. }
  13. if (posInStart < start.length()) {
  14. sequence[posInStart + posInEnd] = ‘i‘;
  15. stack[++posInStack] = start.charAt(posInStart);// push in
  16. dfs(stack, sequence, posInStart + 1, posInEnd, posInStack);
  17. posInStack--;// pop out
  18. }
  19. if (posInStack >= 0 && posInEnd < end.length() && stack[posInStack] == end.charAt(posInEnd)) {// pop out
  20. sequence[posInStart + posInEnd] = ‘o‘;
  21. posInStack--;// pop out
  22. dfs(stack, sequence, posInStart, posInEnd + 1, posInStack);
  23. stack[++posInStack] = end.charAt(posInEnd);
  24. }
  25. }
  26. public static void main(String[] args) {
  27. Scanner sc = new Scanner(new BufferedInputStream(System.in));
  28. while (sc.hasNext()) {
  29. start = sc.next();
  30. end = sc.next();
  31. char[] stack = new char[start.length() * 2];// use array as the stack
  32. char[] sequence = new char[start.length() * 2];// record the operation seq, it will have (start.length() * 2) times operations
  33. System.out.println("[");
  34. dfs(stack, sequence, 0, 0, -1);
  35. System.out.println("]");
  36. }
  37. }
  38. }

最近写的几道题都用到了dfs,用数组代替栈的使用,控制好stack数字下标就没什么大问题,难得一次AC

时间: 2024-08-24 14:05:25

[JAVA][ZOJ 1004][Anagrams by Stack]的相关文章

stack+DFS ZOJ 1004 Anagrams by Stack

题目传送门 1 /* 2 stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 3 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <stack> 9 #include <cmath> 10 #include <cstring> 11 #inc

ZOJ 1004 Anagrams by Stack

Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一个字符串转变成目标字符串的操作,要求输出全部的可能操作组合. 思路:利用深度优先的搜索思路,对于每一个状态都有入栈和出栈两种可能的操作,由于要求按字典序输出,每次先考虑入栈再考虑出栈.即"能入就入,不能入考虑是否能退,随后返回上一步". 下面贴代码: 1 //Problem Name: A

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求的进站出站序列. 就是搜搜搜呗... 带上答案和模拟的栈.. 代码: 1 #include <cstdio> 2 #include <cstdlib> 3 #include <string> 4 #include <iostream> 5 #include &l

ZOJ Problem Set - 1004 Anagrams by Stack (回溯法)

ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i

1004 Anagrams by Stack

考察DFS的应用,用栈描述字符串的变化过程. 1 #include <stdio.h> 2 #include <string.h> 3 int len1,len2; 4 char str1[100],str2[100],stk[100],ans[200]; 5 6 void output(int n){ 7 int i; 8 for(i=0;i<n;i++){ 9 printf("%c ",ans[i]); 10 } 11 printf("\n&

深搜———ZOJ 1004:anagrams by stack

细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深度,npush压栈数,npop出栈数 npush用于记录压栈数:主要判断当前压栈是否合理,以及要压入的元素在原串种的位置 npop用于记录出栈数:判断生成的目标串元素的位置 当npush==npop==目标串时,说明生成了一个可执行的操作串 注意输出操作串的时候一定要用depth参数来控制,因为在多

12、Anagrams by Stack

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i i o o i o ] where i stands for Push and o stands for Pop. Your program should, given pairs

Java异常的栈轨迹(Stack Trace)

捕获到异常时,往往需要进行一些处理.比较简单直接的方式就是打印异常栈轨迹Stack Trace.说起栈轨迹,可能很多人和我一样,第一反应就是printStackTrace()方法.其实除了这个方法,还有一些别的内容也是和栈轨迹有关的. 1.printStackTrace() 首先需要明确,这个方法并不是来自于Exception类.Exception类本身除了定义了几个构造器之外,所有的方法都是从其父类继承过来的.而和异常相关的方法都是从java.lang.Throwable类继承过来的.而pri

【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.内容包括:第1部分 Stack介绍第2部分 Stack源码解析(基于JDK1.6.0_45)第3部分 Vector示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3308852.html 第1部分 Stack介绍 Stack简介 Stack是栈.它的