您的位置:首页 > 其它

Flipper

2015-11-05 16:07 381 查看
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 of
n 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 form
m 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

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.

这道题的题意开始一直没看懂,操作:
L表示最左边的翻转放到其右边的上面,
R是最右边翻转到左边的上面。
控制两个变量左移和右移就行了。

#include <stdio.h>
#include <vector>
#include <string.h>
const int maxn = 105;
struct node
{
int v, is_up;   //is_up表示是否是向上
} t;
std :: vector < node > mp[maxn];    //将vector看成栈就可以了
int a[maxn];
char str[maxn], op[maxn];
void init ( int n )
{
for ( int i = 0; i <= n; i ++ ) //清空
mp[i].clear ( );
for ( int i = 1; i <= n; i ++ )
{
t.v = i;    //初始化
if ( str[i-1] == 'U' )
t.is_up = 1;
else
t.is_up = 0;
mp[i].push_back ( t );
}
}
int main ( )
{
int n, q, l, r, cas = 1;
while ( ~ scanf ( "%d", &n ) && n )
{
scanf ( "%s%s", str, op );
init ( n );
scanf ( "%d", &q );
for ( int i = 1; i <= q; i ++ )
scanf ( "%d", &a[i] );
l = 1, r = n;
printf ( "Pile %d\n", cas ++ );
for ( int i = 0; i < n-1; i ++ )
{
if ( op[i] == 'R' )
{
//将最右边翻转加入到旁边栈中
for ( int j = mp[r].size ( )-1; j >= 0; j -- )
{
t.v = mp[r][j].v;
t.is_up = ! mp[r][j].is_up; //向上变向下
mp[r-1].push_back ( t );
}
mp[r].clear ( );    //清空
r --;   //向左移
}
else
{
for ( int j = mp[l].size ( )-1; j >= 0; j -- )
{
t.v = mp[l][j].v;
t.is_up = ! mp[l][j].is_up;
mp[l+1].push_back ( t );
}
mp[l].clear ( );
l ++;
}
}
//printf ( "%d %d\n", l, r );
for ( int i = 1; i <= q; i ++ )
{
t = mp[l][n-a[i]];
//注意最上面是第一个,所以从下往上是n-a[i]
char ch[12];
if ( t.is_up )
strcpy ( ch, "up" );
else
strcpy ( ch, "down" );
printf ( "Card %d is a face %s %d.\n", a[i], ch, t.v );
}
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: