Sha 10152 ShellSort

Source: Internet
Author: User

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

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.