Joj-1018-Anagrams by stack

xiaoxiao2021-03-06  41

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

I O I I O I O

]

.

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. Both The Two Words Have No More Than 10 Characters. The End of Input is marked by end of file.

OUTPUT

For Each Input Pair, Your Program Sorted List of Valid Sequences of i and o Which Product. Each List Should Be Delimited by

[

]

And The Sequences Should Be Printed in "Dictionary Order". With, Each Sequence, Each

i and

O is Followed by a singne space and each sequence is terminated by a new line.

PROCESS

A stack is a data storage and retrieval structure permitting tour Operations:

Push - to INSERT AN ITEM AND

POP - to Retrieve The Most Recessly 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, the sequence:

iioio ois valid, buti io is not (it's too short), neither isi ioooi (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 iioioo produce the anagram OOF. So also would the sequence iiioo 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.

Sample Input

Madam

ADAMM

Bahama

Bahama

Long

Short

eric

Rice

Sample Output

[

I i i i o o i o o

I I I I O O O I O

I I O I O I O O O

I I O I O I O I O

]

[

I O I I I O I I O O O

I O I I I 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 I O I O I O O O

]

#include

#include

Using namespace std;

BOOL OP [20];

CHAR STRBEFORE [11], Strafter [11], SC [11];

Int Len;

Void Go (int SP, int ID, int idxop, int start)

{

IF (IDX == LEN)

{

For (int i = 0; i

IF (op [i])

COUT << "i";

Else

COUT << "o";

Cout << Endl;

Return;

}

INT OPBAK = IDXOP;

INT SPBAK = SP;

INT I = LEN-1;

While (i> = start)

{

IF (strafter [idx] == strbefore [i])

{

For (int J = start; j <= i; j)

{

OP [IDXOP ] = true;

SC [SP ] = Strbefore [J];

}

OP [IDXOP ] = false;

--SP;

Go (SP, IDX 1, IDXOP, I 1);

IDXOP = OPBAK;

SP = SPBAK;

}

i;

}

IF (sp! = 0 && sc [SP-1] == Strafter [IDX])

{

OP [IDXOP ] = false;

--SP;

Go (SP, IDX 1, IDXOP, START);

}

Else

Return;

}

void main ()

{

While (CIN> Strbefore >> Strafter)

{

COUT << '[' << Endl; if (strlen (strbefore) == Strlen (Strafter)

{

Len = strlen (strbefore);

Go (0, 0, 0, 0);

}

Cout << ']' << Endl;

}

}

Starting with STL's stack almost slowly dead or your stack is fast.

转载请注明原文地址:https://www.9cbs.com/read-75953.html

New Post(0)