hdu1515 DFS stack emulation

Source: Internet
Author: User

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 Descriptionhow 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 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 stacks operations which convert the first word to the second .

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 (in) for Push and O (out) for POPs operations for a initially empty stack of characters. 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 o am 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 o O. You is to write a program to input pairs of words and output all the valid sequences of I and O which would produce the SE Cond member of each pair from the first.

Inputthe 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.

Outputfor each input pair, your program should produce a sorted list of valid sequences of I and o which produce the Targe T 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 new line.
Sample Inputmadamadammbahamabahamalongshortericrice
Sample Output

[
I i i o o o i o O
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 am o o O
I o i o i o i o o i o
]
[
]
[
I i o i o i o O
]


Very interesting topic, stack simulation, through the stack simulation, will be TS1 implementation TS2, the title Requirements dictionary order, I<o, so as long as possible into the stack first.

The topic also pay attention to a problem, the stack may not be empty at last, consider ts1= "ABCDE" ts2= "BCDE" Last ' a ' also stay in the stack, so it is best to empty the stack, write the time did not think, now write the problem suddenly think of the reason ...

This problem is worth repeating research, very classic appearance, long time did not do this kind of DFS problem, can think of half, but I do not see the puzzle can be difficult to clear the mind, in the code

/*programmingts1 all into the stack, the top of the stack and the TS2 pointer to the elements of the comparison, if not match, the top element of the stack pops up, that is, the TS1 pointer forward, continue to match the top of the stack until the ts1 of an element and Ts2 match, stack top elements out of the stack, ts2 pointer back, The original stack of elements continue into the stack, repeat this process, if the TS2 elements are matched completely, the description is feasible, the process of outputting records is because the last operation is out of the stack, so when the output, after the stack to re-enter the stack*/#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<string>Const intINF = (1<< to)-1;Const intMAXN =1e4;using namespaceStd;stack<Char>MS;CharTS1[MAXN];CharTS2[MAXN];intLen1,len2;Charway[2*MAXN];voidDfsintIintJintk) {    if(j==len2) {         for(intL=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 ();//Back out of the stack, total exports    }    if(!Ms.empty ()) {        if(Ms.top () = =Ts2[j]) {Way[k]='o';            Ms.pop (); DFS (I,j+1, K +1); Ms.push (Ts2[j]);//Go back to the stack after the output is finished        }    }}intMain () { 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;}
View Code

hdu1515 DFS stack emulation

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.