hdu1515 dfs栈模拟

Anagrams by Stack

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1513    Accepted Submission(s): 690

Problem Description

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 of words produce sequences of stack operations which convert the first word to the second.

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item 
We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o o is valid, but 
i i o is not (it‘s too short), neither is 
i i o o o i (there‘s an illegal pop of an empty stack)

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.

Input

The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.

Output

For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.

Sample Input

madam
adamm
bahama
bahama
long
short
eric
rice

Sample Output

[
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
]


非常有意思的题目,栈模拟,通过栈模拟,将ts1实现ts2,题目要求字典序,i<o,所以只要尽量进栈优先就可以了

本题还要注意一个问题,栈最后可能不为空,考虑ts1="abcde" ts2="bcde" 最后‘a‘还留在栈中,所以最好要清空栈,写的时候没想到,现在写题解突然想到原因了...

这题值得以后重复研究,挺经典的样子,好久没做这种存dfs的题目了,能想到一半,但是自己不看题解能难理清思绪,题解在代码里

/*programming
ts1全部入栈,栈顶元素和ts2的指针指向的元素比较,如果不匹配,栈顶元素弹出,即ts1的指针前移,继续栈顶匹配,
直到ts1的某个元素和ts2匹配,栈顶元素出栈,ts2的指针后移,原来出栈的元素继续入栈,重复此过程,如果ts2的元素被匹配完全,说明可行,输出记录的过程way
因为最后一步操作是出栈,所以输出的时候,出栈完之后要重新入栈

*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>

const int inf = (1<<31)-1;
const int MAXN = 1e4;

using namespace std;

stack<char>ms;
char ts1[MAXN];
char ts2[MAXN];
int len1,len2;
char way[2*MAXN];

void dfs(int i,int j,int k){
    if(j==len2){
        for(int l=0;l<k;l++){
            cout<<way[l]<<" ";
        }
        cout<<endl;
        return ;
    }
    if(i!=len1){
        ms.push(ts1[i]);
        way[k] = ‘i‘;
        dfs(i+1,j,k+1);
        ms.pop();//回溯出栈,总出口
    }
    if(!ms.empty()){
        if(ms.top()==ts2[j]){
            way[k] =‘o‘;
            ms.pop();
            dfs(i,j+1,k+1);
            ms.push(ts2[j]);//输出完之后回溯进栈
        }
    }
}

int main()
{
    while(scanf("%s%s",ts1,ts2)!=EOF){
        len1 = strlen(ts1);
        len2 = strlen(ts2);
        cout<<"["<<endl;
        dfs(0,0,0);
        cout<<"]"<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

时间: 2024-09-30 15:36:58

hdu1515 dfs栈模拟的相关文章

Code POJ - 1780(栈模拟dfs)

题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector&g

深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main() {while(在图中能找到未访问过的点 k) Dfs(k);} 例题: POJ1164 The Castle Description 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#-

用两个栈模拟实现一个队列

题目:如何用两个栈模拟实现一个队列?  如果这两个堆栈的容量分别是m和n(m>n),你的方法能保证的队列容量是多少?(这里讨论的是顺序栈,如果是链式栈的话完全没有必要考虑空间) 分析:栈的特点是“后进先出(LIFO)”,而队列的特点是“先进先出(FIFO)”.用两个栈模拟实现一个队列的基本思路是:用一个栈作为存储空间,另一个栈作为输出缓冲区,把元素按顺序压入两栈(模拟的队列),并按此顺序出队并输出即可. 如下图,用容量为m的栈作为存储空间,容量为n的栈作为输出缓冲区,一开始先将n个元素压入(pu

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

ACM学习历程——UVA 127 &quot;Accordian&quot; Patience(栈;模拟)

Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate n

HDOJ 4699 Editor 栈 模拟

用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1913    Accepted Submission(s): 591 Problem Description Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 Hint The fol

web前端面试系列 - 数据结构(两个栈模拟一个队列)

一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将剩余数据弹出s2,并压人s1. 思路二: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时,判断s1, 是否为空,如果不为空,则将数据直接压入s1, 如果为空,则将s2中的数据全部倒入s1,在将数据压人s1. 3. 出队时,判断s2, 是否为空,如果不为空,则直接弹出s2

自定义栈的实现及使用两个栈模拟队列

一,使用单链表实现栈 ①栈需要一个栈顶指针 ②栈的基本操作有出栈和入栈,以及判断栈是否为空 ③单链表中每个结点表示一个栈元素,每个结点有指向下一个结点的指针.因此,在栈内部需要实现一个单链表.代码如下: public class Stack<T extends Comparable<? super T>>{ private class Node{ T ele; Node next; public Node(T ele) { this.ele = ele; } } Node top;

【干货】容器适配器实现两个栈模拟队列

用两个栈模拟队列的思想就是"倒水思想",这里我们用自定义类型模拟出线性表,再用线性表做容器实现栈的数据结构,最后用栈来实现队列,代码如下: #include<iostream> #include<string> #include<cassert> struct __TrueType//类型萃取 { bool Get() { return true; } }; struct __FalseType { bool Get() { return false