uva10152
2015-10-25 15:24
330 查看
题目:
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
题目大概意思:
使用最少的步数移动可以使得前面m个序列变成后面m个序列。
思路:
将前面M个放入数组中,然后将后面的M个放入栈中,这样刚好可以从尾巴开始查找(从尾巴开始查找可以达到最少步数),如果数组中的和栈中的一样就将栈中的pop出来,最后留下来的就是需要移动的乌龟。输出后,就是要移动的顺序。
代码:
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
题目大概意思:
使用最少的步数移动可以使得前面m个序列变成后面m个序列。
思路:
将前面M个放入数组中,然后将后面的M个放入栈中,这样刚好可以从尾巴开始查找(从尾巴开始查找可以达到最少步数),如果数组中的和栈中的一样就将栈中的pop出来,最后留下来的就是需要移动的乌龟。输出后,就是要移动的顺序。
代码:
#include <iostream> #include <stack> #include <string> using namespace std; string ss[300]; int main() { int n,i; string s; stack<string> s1; int left; cin >> n; while (n--) { int m; cin >> m; getchar(); for (i = 0; i < m * 2; i++) { getline(cin,s); if (i < m) ss[i] = s; else s1.push(s); } left = m-1; while (!s1.empty() && left >= 0) { if (ss[left] == s1.top()) { s1.pop(); } left--; } while(!s1.empty()) { cout << s1.top() << endl; s1.pop(); } printf("\n"); } return 0; }
相关文章推荐
- http协议之响应
- 机器人搬重物
- 用Logger来解释拦截
- java实现获取用户的MAC地址
- win7 安装64 MySQL
- 关于cdb与pdb原始用户的问题
- 约瑟夫环java实现的方法
- 用两个栈实现队列 c++ 9度题目号1512
- 连接池 druid(阿里巴巴的框架)
- 什么是面向过程,什么是面向对象?
- Android版本控制软件之SVN
- 位,字和字节的关系电脑知识
- Code P3286 火柴排队 2013年NOIP全国联赛提高组
- pb现在还有用的吗?
- C/C++专项练习 (四)
- 用小猫统计定制的中小学教师专业水平评价证书编辑与批量打印程序
- 排行榜
- [转载]用 grub2 启动 clover.iso 来启动 OS X
- sqlmap注入检测经验0x01
- CSS3 animation 属性