Flipper(杭电3328)(模拟栈)
2015-12-23 16:00
239 查看
Flipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 693 Accepted Submission(s): 421
Problem Description
Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts with n cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (Card 1 is on the far left;
card n is on the far right.) Some cards are face up and some are face down. Bobby then performs n - 1 flips — either right flips or left flips. In a right flip he takes the pile to the far right and flips it over onto the card to its immediate
left. For example, if the rightmost pile has cards A, B, C (from top to bottom) and card D is to the immediate left, then flipping the pile over onto card D would result in a pile of 4 cards: C, B, A, D (from top to bottom). A left flip is analogous.
The very last flip performed will result in one pile of cards — some face up, some face down. For example, suppose Bobby deals out 5 cards (numbered 1 through 5) with cards 1 through 3 initially face up and cards 4 and 5 initially face down. If Bobby performs
2 right flips, then 2 left flips, the pile will be (from top to bottom) a face down 2, a face up 1, a face up 4, a face down 5, and a face up 3.
Now Bobby is very sharp and you can ask him what card is in any position and he can tell you!!! You will write a program that matches Bobby’s amazing feat.
Input
Each test case will consist of 4 lines. The first line will be a positive integer n (2 ≤ n ≤ 100) which is the number of cards laid out. The second line will be a string ofn characters. A character U indicates the corresponding card
is dealt face up and a character D indicates the card is face down. The third line is a string of n - 1 characters indicating the order of the flips Bobby performs. Each character is either R, indicating a right flip, or L, indicating a left flip.
The fourth line is of the formm q1 q2 . . . qm, where m is a positive integer and 1 ≤ qi ≤ n. Each qi is a query on a position of a card in the pile (1 being
the top card, n being the bottom card). A line containing 0 indicates end of input.
Output
Each test case should generate m + 1 lines of output. The first line is of the form
Pile t
where t is the number of the test case (starting at 1). Each of the next m lines should be of the form
Card qi is a face up k.
or
Card qi is a face down k.
accordingly, for i = 1, ..,m, where k is the number of the card.
For instance, in the above example with 5 cards, if qi = 3, then the answer would be
Card 3 is a face up 4.
Sample Input
5 UUUDD RRLL 5 1 2 3 4 5 10 UUDDUUDDUU LLLRRRLRL 4 3 7 6 1 0
Sample Output
[code]Pile 1
Card 1 is a face down 2.
Card 2 is a face up 1.
Card 3 is a face up 4.
Card 4 is a face down 5.
Card 5 is a face up 3.
Pile 2
Card 3 is a face down 1.
Card 7 is a face down 9.
Card 6 is a face up 7.
Card 1 is a face down 5.
题意:给你n张编号为1~n的(正反两面)牌放在桌子上。初始时有一个状态(U代表数字朝上,D代表数字朝下),接下来有n-1个操作。L代表将最左边的牌翻转放到旁边牌的最上方,R代表将最右边的牌翻转放到旁边牌的最上方。输出最终状态牌和状态;
比如所给第一组测试数据:初始状态 (正)1,(正)2,(正)3,(反)4,反(5);接下来:R之后:(正)1,(正)2,(正)3,(正)5(反)4; R之后:(正)1,(正)2,(正)4(反)5(正)3; L之后:(反)1(正)2,(正)4(反)5(正)3; L之后:(反)2(正)1(正)4(反)5(正)3;
代码:
#include<stdio.h> #include<string.h> #include<stack> using namespace std; int main() { int i,j,k,n,m,l,r,kase=1; char a[101]; char b[101]; int vis[101]; int num[101]; stack<int> s[101]; while(scanf("%d",&n),n) { for(i=0;i<n;i++) { s[i].push(i); } scanf("%s",a); for(i=0;i<n;i++) { if(a[i]=='U') vis[i]=1; else vis[i]=-1; } l=0,r=n-1;//l,r来控制牌向右摞时需要翻面的个数 scanf("%s",b); for(i=0;i<n-1;i++) { if(b[i]=='L') { for(j=0;j<=l;j++) vis[j]=-1*vis[j]; l++; while(!s[l-1].empty())// { s[l].push(s[l-1].top()); s[l-1].pop(); } } else { for(j=n-1;j>=r;j--) vis[j]=-1*vis[j]; r--; while(!s[r+1].empty()) { s[r].push(s[r+1].top()); s[r+1].pop(); } } } for(i=0;i<n;i++)//将最后一摞牌存入数组; { num[i]=s[l].top(); s[l].pop(); } printf("Pile %d\n",kase++); scanf("%d",&m); while(m--) { scanf("%d",&k); printf("Card %d is a face ",k); printf(vis[num[k-1]]==1?"up":"down"); printf(" %d.\n",num[k-1]+1); } } }
相关文章推荐
- 工具--返回日期格式(给定一个long值)
- [Unity] 从SurvivalShooter的角色移动,看FixedUpdate与Update
- poj3468 A Simple Problem with Integers(线段树)
- mysql mha高可用架构的安装
- 老李推荐:第2章2节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NotesList简介
- Unable to delete file/directory
- 返回N协议与选择重传协议的发送窗口大小问题
- SQLLDR导入乱码问题的解决
- eclipse修改SVN账号密码
- 64bits Python2.7.5安装numpy包
- html5代码,获取地理位置
- 老李推荐:第2章1节《MonkeyRunner源码剖析》了解你的测试对象: NotePad应用简介
- 切换oracle提示Too many open files in system
- springboot-入门注意事项
- iOS AES 256加密
- ROS系统命令学习
- 使用Kettle同步数据
- bootstrap下拉菜单与滚动监视器学习笔记(2-2 下拉菜单--属性声明式方法(一))
- oracle rman备份
- redhat6.5文件共享