poj3087(模拟)Shuffle'm Up(简单学习一下map)

本文介绍了一个关于洗牌算法的比赛题目,玩家需要通过一系列操作将两堆扑克芯片混合并重新分配,以达到特定的目标状态。文章详细解释了洗牌过程,并提出了一种算法来判断是否能通过多次洗牌达到期望的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shuffle'm Up
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9277 Accepted: 4256

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, S1 and S2, 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 S1 with a chip from S2 as shown below for C = 5:

The single resultant(结果的) stack, S12, contains 2 * C chips. The bottommost(最低的) chip of S12 is the bottommost(最低的) chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving(交错) process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

After the shuffle(洗牌) operation, S12 is split into 2 new stacks(堆) by taking the bottommost(最低的) C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle(洗牌) operation may then be repeated to form a new S12.

For this problem, you will write a program to determine if a particular resultant(结果的) stack(堆) S12 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 ((堆)S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost(最低的) chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 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 S1 and S2 zero or more times. The bottommost chip’s color is specified f(指定)irst.

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

#include<stdio.h>
#include<map>
#include<string>
#include<string.h>
using namespace std;
int main()
{
    int t;
    int n;
    char s1[1000],s2[1000],s12[1000],ss[1000];
    scanf("%d",&t);
    int cou=0;
    while(t--)
    {
        cou++;
        int flag=0;
        scanf("%d",&n);
        map<string,bool>vis;
        scanf("%s%s%s",s1,s2,ss);

        vis[ss]=1;
        int step=0;
        while(1)
        {
               step++;
               int c=0;
               for(int i=0;i<n;i++)
              {
                s12[c++]=s2[i];
                s12[c++]=s1[i];
              }
               s12[c]='\0';
              if(strcmp(s12,ss)==0)
              {
                  flag=1;
                  //printf("%d\n",step);
                  break;
              }

              if(vis[s12]==1)
              {

                  //printf("-1\n");
                  break;
              }

              vis[s12]=1;

              for(int i=0;i<n;i++)
              {
                  s1[i]=s12[i];

              }

               c=0;
              for(int i=n;i<2*n;i++)
              {
                  s2[c++]=s12[i];
              }
              s2[c]='\0';
        }
        if(flag==1)
        printf("%d %d\n",cou,step);
        else printf("%d -1\n",cou);

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值