zoukankan      html  css  js  c++  java
  • UVALive4536 POJ3824 HDU3328 Flipper【模拟+堆栈】

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 106   Accepted: 52

    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 ?ips, 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.

    Source


    Regionals 2009 >> North America - East Central NA


    问题链接UVALive4536 POJ3824 HDU3328 Flipper

    问题简述参见上文。

    问题分析这是一个模拟题,用堆栈等实现。先占个位置,细节以后再解释

    程序说明:(略)

    题记:(略)

    参考链接:(略)


    AC的C++语言程序如下:

    /* UVALive4536 POJ3824 HDU3328 Flipper */
    
    #include <iostream>
    #include <stack>
    #include <stdio.h>
    
    using namespace std;
    
    const int N = 100;
    
    stack<int> s[N];
    int a[N];
    
    int main()
    {
        int n, pno = 0, m, k, j;
        string cards, flips;
    
        while(cin >> n && n) {
            cin >> cards;
    
            j = 0;
            while(cards[j]) {
                cards[j] = (cards[j] == 'U') ? 1 : 0;
                j++;
            }
    
            for(int i=0; i<n; i++)
                s[i].push(i);
    
            int left = 0, right = n - 1;
            cin >> flips;
            for(int i=0; i<n-1; i++) {
                if(flips[i] == 'L') {
                    left++;
                    for(int j=0; j<left; j++)
                        cards[j] = 1 - cards[j];
                    while(!s[left - 1].empty()) {
                        s[left].push(s[left - 1].top());
                        s[left - 1].pop();
                    }
                } else if(flips[i] == 'R') {
                    right--;
                    for(int j=n-1; j>right; j--)
                        cards[j] = 1 - cards[j];
                    while(!s[right + 1].empty()) {
                      s[right].push(s[right + 1].top());
                      s[right + 1].pop();
                    }
                }
            }
    
            for(int i=0; i<n; i++) {
                a[i] = s[left].top();
                s[left].pop();
            }
    
            cout << "Pile " << ++pno << endl;
    
            cin >> m;
            while(m--) {
                cin >> k;
                printf("Card %d is a face ", k);
                k--;
                printf("%s %d.
    ", (cards[a[k]] ? "up" : "down"), a[k] + 1);
            }
        }
    
        return 0;
    }





  • 相关阅读:
    菜鸟级别学习
    BootSrap学习
    将一正整数序列{K1,K2,...,K9}重新排列成一个新的序列。新序列中,比K1小的数都在K1的前面(左面),比K1大的数都在K1的后面(右面)。
    按递增顺序依次列出所有分母为40,分子小于40的最简分数。
    对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串"(max)"。
    有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的。
    创建一个带头结点的单链表,逆置链表,在单链表中删除值相同的多余结点,并遍历链表,删除链表最大节点。
    从键盘输入一个字符串,按照字符顺序从小到大进行选择排序,并要求删除重复的字符
    数组的逆置
    求亲密数
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563571.html
Copyright © 2011-2022 走看看