There are a bunch of turtles, output a bunch of disordered turtles, and then enter a correct order, asking for a minimum step to make the first heap into the second heap, every time you move the tortoise to the first position, output this step.
Solution: We can use two char arrays to store the turtle sequence of the first and second heaps, and then use two int arrays num and tem to store the sequential numbers of turtles. we know that we can find the smallest step by traversing the tem array from the back, and then use the advanced and later properties of the stack (From the above we can see that the last one of the turtles must be the first to move)
Code:
[Cpp]
// UVA10152
# Include <cstdio>
# Include <cstring>
# Include <iostream>
# Include <stack>
# Include <map>
# Include <algorithm>
Using namespace std;
Char ch [210] [100], temp [210] [100];
Int num [210], tem [210];
Void output (int n ){
Int I, j;
Memset (tem, 0, sizeof (tem ));
For (I = 0; I <n; I ++ ){
For (j = 0; j <n; j ++ ){
If (strcmp (temp [I], ch [j]) = 0)
Tem [I] = num [j];
}
}
For (I = n-2; I> = 0; I --){
If (tem [I]> tem [I + 1])
Break;
}
Stack <char *> s;
For (j = 0; j <= I; j ++)
S. push (temp [j]);
While (! S. empty ()){
Cout <s. top () <endl;
S. pop ();
}
Cout <endl;
}
Int main (){
Int t, n, I, j;
Cin> t;
While (t --){
Int n;
Cin> n;
Getchar ();
Memset (num, 0, sizeof (num ));
For (I = 0; I <n; I ++ ){
Gets (ch [I]);
Num [I] = I;
}
For (I = 0; I <n; I ++ ){
Gets (temp [I]);
}
Output (n );
}
Return 0;
}
Author: cgl1079743846