加载中...

POJ 3087 - Shuffle'm Up


0x00 POJ 原题

Description

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S_(1) and S_(2), each stack containing C chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S_(1) with a chip from S_(2) as shown below for C = 5:

The single resultant stack, S_(12), contains 2 * C chips. The bottommost chip of S_(12) is the bottommost chip from S_(2). On top of that chip, is the bottommost chip from S_(1). The interleaving process continues taking the 2^(nd) chip from the bottom of S_(2) and placing that on S_(12), followed by the 2^(nd) chip from the bottom of S_(1) and so on until the topmost chip from S_(1) is placed on top of S_(12).

After the shuffle operation, S_(12) is split into 2 new stacks by taking the bottommost C chips from S_(12) to form a new S_(1) and the topmost C chips from S_(12) to form a new S_(2). The shuffle operation may then be repeated to form a new S_(12).

For this problem, you will write a program to determine if a particular resultant stack S_(12) can be formed by shuffling two stacks some number of times.

Input

The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S_(1) and S_(2)). The second line of each dataset specifies the colors of each of the C chips in stack S_(1), starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S_(2) starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S_(1) and S_(2) zero or more times. The bottommost chip’s color is specified first.

Output

Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

Sample Input

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

Source

Greater New York 2006

0x10 题目概述

已知两堆牌 s1 和 s2 的初始状态,牌数均为 c,按给定规则能将他们相互交叉组合成一堆牌 s12,再将 s12 的最底下的 c 块牌归为 s1,最顶的 c 块牌归为 s2,依此循环下去。

现在输入 s1 和 s2 的初始状态 以及 预想的最终状态 s12

问 s1 s2 经过多少次洗牌之后,最终能达到状态 s12,若永远不可能相同,则输出”-1”。

0x20 解题思路

很浅白的模拟题

直接模拟就可以了,关键在于状态记录,然后判重

若 s1 和 s2 在洗牌后的状态,是前面洗牌时已经出现过的一个状态,且这个状态不是预想的状态 S12,就说明无论怎样再洗牌都不可能达到 S12 了,因为这个洗牌操作已经陷入了一个“环”

如果状态没有重复过,则一直模拟洗牌,直至 s12 出现

记录状态可以用 map<string,bool>vist, Map 的缺省值为 0

知道这个就不难了

0x30 AC 源码

//Memory Time 
//204K   0MS 

#include<iostream>
#include<string>
#include<map>
using namespace std;

int main(int i,int k)
{
    int test,c;
    int t=0;
    cin>>test;
    while(++t<=test)
    {
        cin>>c;
        char s1[201];   //牌堆1
        char s2[201];   //牌堆2
        char s12[401];  //预设最终的牌堆状态
        cin>>s1>>s2>>s12;

        map<string,bool>vist;   //记录出现过的洗牌状态(map缺省值为0)
        vist[s12]=true;

        int step=0;  //洗牌次数
        while(true)
        {
            char s[201];  //当前s1与s2洗牌后的状态
            int ps=0;  //s[]指针
            for(i=0;i<c;i++)    //s1与s2洗牌
            {
                s[ps++]=s2[i];
                s[ps++]=s1[i];
            }
            s[ps]='\0';

            step++;

            if(!strcmp(s,s12))   //当洗牌后的状态能达到预设状态时,输出
            {
                cout<<t<<' '<<step<<endl;
                break;
            }
            else if(vist[s] && strcmp(s,s12))  //当前洗牌后状态 与 前面出现过的状态重复了,但该状态不是预设状态
            {                                  //说明预设的状态无法达到
                cout<<t<<' '<<-1<<endl;
                break;
            }

            vist[s]=true;   //状态记录

            for(i=0;i<c;i++)   //分拆出s1与s2
                s1[i]=s[i];
            s1[i]='\0';

            for(k=0;i<2*c;i++,k++)
                s2[k]=s[i];
            s2[i]='\0';
        }
    }
    return 0;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录