Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&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.
The first line of the input consists of a single integer K giving the number of test cases. Each test case consist on an integerngiving 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 nextnlines 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.
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.
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
Duke of Earl Sir Lancelot Richard M. Nixon Yertle
思路:首先找到需要移动的字符串,方法如下:以初始序列为准,设初始序列下标为i, 目的序列下标为j, 从n-1开始,如果两下标对应的字符串相等,下标同时减一,否则仅初始序列下标减一。那么目的序列中还未被成功匹配的字符串就是需要移动的字符串。要使移动次数最少,显然应该按未被处理的目的序列中字符串逆序移动(输出)。
此想法来自:http://www.cppblog.com/wuxu/archive/2011/11/22/160719.aspx
完整代码:
/*0.038s*/ #include<cstdio> #include<cstring> char origin[210][100], require[210][100]; int main() { int cas, i, j, n; scanf("%d\n", &cas); while (cas--) { scanf("%d\n", &n); for (i = 0; i < n; i++) gets(origin[i]); for (i = 0; i < n; i++) gets(require[i]); for (i = n - 1, j = n - 1; i >= 0 && j >= 0;) { if (strcmp(origin[i], require[j]) == 0) i--, j--; else i--; } while (j >= 0) puts(require[j--]); putchar(10); } return 0; }