zoukankan      html  css  js  c++  java
  • Shuffle'm Up 纯模拟,注意跳出条件,否则陷入死循环

    Problem 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 andS2, 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 (S1and 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 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
    ***************************************************************************************************************************
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 using namespace std;
     7 char str1[1001],str2[1001],str3[1001];
     8 int cas,lent,k;
     9 void shuffle(char*a,char*b,char*c)//分牌
    10   {
    11     int len=strlen(a);
    12     int it,jt;
    13     for(it=0,jt=0;jt<len;it+=2,jt++)
    14     {
    15         c[it]=b[jt];
    16         c[it+1]=a[jt];
    17     }
    18   }
    19 void cipai(char*a,char*b,char*c)//洗牌
    20  {
    21      int len=strlen(c);
    22      int it,jt;
    23      for(it=0;it<len/2;it++)
    24       a[it]=c[it];
    25      for(it=len/2,jt=0;it<len;it++,jt++)
    26       b[jt]=c[it];
    27  }
    28 int main()
    29  {
    30      k=0;
    31      scanf("%d",&cas);
    32      while(cas--)
    33      {
    34          k++;
    35          scanf("%d",&lent);
    36          getchar();
    37          scanf("%s",str1);
    38          scanf("%s",str2);
    39          scanf("%s",str3);
    40          char temp1[1001],temp2[1001],temp3[1001];
    41          strcpy(temp1,str1);
    42          strcpy(temp2,str2);
    43          strcpy(temp3,str3);
    44          int sg=1,step=0;
    45          while(1)//模拟
    46          {
    47              shuffle(temp1,temp2,temp3);
    48              step++;
    49              if(strcmp(temp3,str3)==0)//如果满足条件跳出
    50              {
    51                  break;
    52              }
    53              cipai(temp1,temp2,temp3);
    54              if((strcmp(str1,temp1)==0)&&(strcmp(str2,temp2)==0))//如果陷入死循环,跳出
    55              {
    56                  sg=0;
    57                  break;
    58              }
    59          }
    60          printf("%d %d
    ",k,((sg==1)?step:-1));
    61 
    62      }
    63  }
    View Code
  • 相关阅读:
    Redis持久化之RDB
    linux中查看进程中的线程
    Redis客户端
    Redis之GEO
    Redis之发布订阅
    Redis之HyperLogLog
    CSP-S2020游记
    根据表名 查询 表的列,备注,类型等 SQL
    mybatis-plus的使用 ------ 入门
    IntelliJ IDEA 版本控制(svn、git) 修改文件后,所属目录的颜色也变化
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3372792.html
Copyright © 2011-2022 走看看