Va OJ 732-anagrams by stack)

Source: Internet
Author: User

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

 

Background
Background

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert "trot" to "tort ":
How can I re-arrange strings in the required order through a series of stack operations?For example, there are two types of inbound and outbound stack sequences.Convert the string "trot" to "tort" as follows:

[
I O
I o I o
]

Where I stands for push and O stands for Pop. Your program shocould, given pairs of words produce sequences of stack operations which convert the first word to the second.
I indicates press-in, and O indicates pop-up. You need to write a pair of words.ProgramThrough a group of operations in and out of the stack, the alphabetic order of the previous word is changed to the last word.

 

Input
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 input consists of multiple rows, each of which occupies 2 rows of words. 1st act the original word (excluding the line break at the end of the line) and 2nd act the target word (also not including the line break at the end of the line ).

 

Output
Output

For each input pair, your program showould produce a sorted list of valid sequences of I and O which produce the target word from the source word. Each list shocould be delimited
For each pair of input strings, your program should generate a list of all valid inbound and outbound stack operations in sequence. Each group of operations can convert the original string to the target string. Each group of operations should be enclosed in square brackets:

[
]

And the sequences shoshould 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.
Each group of operations in the sequence should be output alphabetically. In a group of operations, I and O are separated by spaces, each group occupies an exclusive row.

 

Process
Processing

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

    • Push-to insert an item and
      Press-insert an item into the stack
    • Pop-to retrieve the most recently pushed item
      Pop-up -- retrieve the last inserted 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:
You need to use "I" to indicate stack entry, "O" to indicate stack exit, and the initial status of the stack is empty. Given an input word, this group of operations can be effective as long as each letter has been pushed into and out of the stack. In addition, the pop-up operation cannot be performed on an empty stack. For example, for the input word Foo, the following operation sequence:

    • I o I o is valid,
      I O is valid,
    • I O is not (it's too short ),
      I O is invalid (operation not completed)
    • Neither is
      I o I (There's an illegal pop of an empty stack)

      I o I is also invalid (the operation popped up from the empty stack is invalid)

Valid sequences yield rearrangements of the letters in an input word. for example, the input word Foo and the sequence I I o produce the anw.oof. so also wowould the sequence I 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.
Multiple groups of valid operations may generate a specified letter arrangement for the input words. For example, for the input word "foo", the operation I O can generate the rearranged word "Oof ". Operation I O can also generate the same arrangement. You need to write a program to find out all the valid operation sequences, so that the former of the input pair of words is rearranged as the latter.

 

Sample Input
Input example

Madam
Adamm
Bahama
Bahama
Long
Short
Eric
Rice

 

Sample output
Output example

[
I O
I O
I O
I o I o
]
[
I o I I o I O
I o I o I O
I O
I o I o
]
[
]
[
I O
]

 

Analysis
Analysis

This question can be solved only when all possible inbound and outbound stack sequences are repeated. If the sequence of inbound and outbound Stacks is treated as a binary tree, the solution process is a typical in-depth example. In the stack, the left subnode and the right subnode are the output stack. When all characters have been output from the stack, that is, when the samples are sent to the leaf node, print the previous inbound and outbound stack sequence (or the path from the root to the leaf ). If the pop-up character on the stack is found to be inconsistent with the position of the character in the target string, you do not need to repeat the subnode of the example again and simply go back to it. The idea is very simple, so we can see the implementation. Note: Do not output spaces at the end of the line.

 

Solution
Answer
# Include <iostream> # include <string> # include <vector> using namespace STD; // all possible inbound and outbound stack sequences of deep examples. Stack feed is considered as the left subtree, the output stack is considered as the right subtree void genanagram (vector <char> & SRC, vector <char> & DEST, vector <char> & Order, vector <char> & stack) {// If the string is not empty, perform the stack import operation, which is equivalent to the left subtree if (! SRC. empty () {// Add iorder to the operation sequence. push_back ('I'); // stack and save the live stack. push_back (SRC. back (); SRC. pop_back (); // uses the current status as an example. The next action is genan=( SRC, DEST, order, stack); // restores the SRC. push_back (stack. back (); stack. pop_back (); Order. pop_back () ;}// if the stack is not empty, the stack should be played. If (! Stack. empty () & stack. back () = Dest [stack. size () + SRC. size ()-1]) {// Add Oorder to the operation sequence. push_back ('O'); // stack and save the field char ctemp = stack. back (); stack. pop_back (); // uses the current status as an example. The next action is genancript (SRC, DEST, order, stack); // restores the on-site stack. push_back (ctemp); Order. pop_back ();} // if the original string is empty and the stack is the same, all characters have been output. If (SRC. empty () & stack. empty () {// output the sequence of inbound and outbound stack operations vector <char >:: iterator I = order. begin (); For (; I! = Order. end ()-1; ++ I) {cout <* I <'';} cout <* I <Endl ;}} // main function int main (void) {// process each pair of input strings for (string str1, str2; CIN >>> str1 >> str2; cout <']' <Endl) {cout <'[' <Endl; // Calculate if (str1.length () only when the two strings are of the same length and are not empty () = str2.length ()&&! Str1.empty () {// create the original string, target string, character stack, and result array vector <char> SRC (str1.rbegin (), str1.rend ()); vector <char> DEST (str2.rbegin (), str2.rend (); vector <char> order, stack; // execute an in-depth example and output all results genanagram (SRC, DEST, order, stack) ;}} return 0 ;}

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.