Zoj1004-anagrams by Stack "stack backtracking"

Source: Internet
Author: User

Http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4

Anagrams by Stack Time limit: 2 Seconds Memory Limit: 65536 KB

How can anagrams result from sequences of stack operations? There is sequences of stack operators which can convert TROT to TORT:

[I i i i o o o oi 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 stacks operations which convert the first word to the second .

Input

The input would consist of several lines of input. The first line of each pair of input lines are to being considered as a source word (which does not include the End-of-line CH Aracter). The second line (again, not including the end-of-line character) of all 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 produ Ce the target word from the source word. Each list should is delimited by

[]

And the sequences should is printed in "Dictionary order". Within each sequence, each i and o are followed by a single space and each sequence are terminated by a NE W line.

Process

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

push-to Insert an item and

Pop-to Retrieve the most recently pushed item

We'll use the symbol I (on) for push and o (out) for pop operations for initially empty stack of CH Aracters. Given an input word, some sequences of push and pop operations be valid in that every character of the word is both Pushe D and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO was 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 i (there ' s an illegal pops 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 oo. You is to write a program to input pairs of words and output all the valid sequences of i and o which W Ill produce the second member of each pair from the first.

Sample Input
Madamadammbahamabahamalongshortericrice
Sample Output
[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 o o o i o i o i o i o i o i o] [][i i o i o O]






Test instructions: The last line of words is changed from the stack to the next line of words, and all methods are output in dictionary order.
Ideas: Stacks + backtracking
Code:
1#include <fstream>2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cmath>7#include <cstdlib>8 9 using namespacestd;Ten  One #defineEPS 1e-6 A #definell Long Long - #defineINF 0x7fffffff -  the Const intn= -; -  - Chars1[n],s2[n],r1[2*n],sta[2*N]; - intL1,l2,rl,top; +  - voidDfs (intIpintOP);//IP represents the number of stacks, OP indicates the number of stacks +  A intMain () at { -     //freopen ("d:\\input.in", "R", stdin); -      while(~SCANF ("%s%s", S1,S2)) { -Puts"["); -l1=strlen (S1); -L2=strlen (S2); inrl=top=-1; -top=0; to         if(L1==L2) Dfs (0,0); +Puts"]"); -     } the     return 0; * } $ voidDfs (intIpintop) {Panax Notoginseng     if(ip==l1&&op==L2) { -          for(intI=0; i<=rl;i++){ theprintf"%c", R1[i]); +         } APuts""); the     } +     if(ip<L1) { -sta[++top]=S1[ip]; $r1[++rl]='I'; $Dfs (ip+1, op); -top--; -rl--; the     } -     if(top>=0&&sta[top]==S2[op]) {Wuyitop--; ther1[++rl]='o'; -Dfs (ip,op+1); Wusta[++top]=S2[op]; -rl--; About     } $}
View Code

Zoj1004-anagrams by Stack "stack backtracking"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.