Algorithm


problem Link  : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1093 

He made each turtle stand on another one’s back And he piled them all up in a nine-turtle stack. And then Yertle climbed up. He sat down on the pile. What a wonderful view! He could see ’most a mile! King Yertle wishes to rearrange his turtle throne to place his highest-ranking nobles and closest advisors nearer to the top. A single operation is available to change the order of the turtles in the stack: a turtle can crawl out of its position in the stack and climb up over the other turtles to sit on the top. Given an original ordering of a turtle stack and a required ordering for the same turtle stack, your job is to determine a minimal sequence of operations that rearranges the original stack into the required stack. Input The first line of the input consists of a single integer K giving the number of test cases. Each test case consist on an integer n giving the number of turtles in the stack. The next n lines specify the original ordering of the turtle stack. Each of the lines contains the name of a turtle, starting with the turtle on the top of the stack and working down to the turtle at the bottom of the stack. Turtles have unique names, each of which is a string of no more than eighty characters drawn from a character set consisting of the alphanumeric characters, the space character and the period (‘.’). The next n lines in the input gives the desired ordering of the stack, once again by naming turtles from top to bottom. Each test case consists of exactly 2n + 1 lines in total. The number of turtles (n) will be less than or equal to two hundred. Output For each test case, the output consists of a sequence of turtle names, one per line, indicating the order in which turtles are to leave their positions in the stack and crawl to the top. This sequence of operations should transform the original stack into the required stack and should be as short as possible. If more than one solution of shortest length is possible, any of the solutions may be reported. Print a blank line after each test case. Sample Input 2 3 Yertle Duke of Earl Sir Lancelot Duke of Earl Yertle Sir Lancelot 9 Yertle Duke of Earl Sir Lancelot Elizabeth Windsor Michael Eisner Richard M. Nixon Mr. Rogers Ford Perfect Mack Yertle Richard M. Nixon Sir Lancelot Duke of Earl Elizabeth Windsor Michael Eisner Mr. Rogers Ford Perfect Mack Sample Output Duke of Earl Sir Lancelot Richard M. Nixon Yertle

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <bits/stdc++.h>
using namespace std;

int main() {
    int t,n;
    string in;
    cin >> t;
    while(t--){
        cin >> n;
        cin.ignore();
        vector < string> original, expected;
        for(int i=0;i < 2*n;i++){
            getline(cin,in);
            if(i<n) original.push_back(in);
            else expected.push_back(in>;
        }
        // start from b, compare
        int i=n-1,j=n-1;
        for(;i>=0;i--)
            // since they have a match, advance to next
            if(original[i] == expected[j]) j--;
        // sort the rest by putting them on top
        while(j>=0){
            cout << expected[j--] << endl;
        }
        cout << endl;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
3
Yertle
Duke of Earl
Sir Lancelot
Duke of Earl
Yertle
Sir Lancelot
9
Yertle
Duke of Earl
Sir Lancelot
Elizabeth Windsor
Michael Eisner
Richard M. Nixon
Mr. Rogers
Ford Perfect
Mack
Yertle
Richard M. Nixon
Sir Lancelot
Duke of Earl

Michael Eisner
Mr. Rogers
Ford Perfect
Mack

Output

x
+
cmd
Duke of Earl
Sir Lancelot
Richard M. Nixon
Yertle
Advertisements

Demonstration


 UVA Online Judge solution - 10152-ShellSort - UVA Online Judge solution in C,C++,Java

Previous
UVA Online Judge solution - 10149 - Yahtzee - UVA Online Judge solution in C,C++,Java
Next
UVA Online Judge solution - 10154 - Weights and Measures - UVA Online Judge solution in C,C++,Java