zoukankan      html  css  js  c++  java
  • POJ3087 Shuffle'm Up —— 打表找规律 / map判重

    题目链接:http://poj.org/problem?id=3087


    Shuffle'm Up
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12305   Accepted: 5708

    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 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



    题解:

    方法一:经打表发现,当经过2n次洗牌之后,必定能回到初始状态,所以直接枚举到2n。 问:为何经过2*n后必定能回到初始状态?自己也不会证明,但有一点:对于一个数来说,它所在的位置只有2n种情况。应该与这个有关。复杂度计算:1000(测试组数)*200(2n)*200(字符串长度) = 4e7,如果真的是这个计算次数的话,是会超时的,但可能数据比较弱。


    方法二:直接模拟,然后加个map判重,如果某个状态出现过,并且不是目标状态,则可以得出结论:不可能得到目标状态。因为洗牌的状态是会不断循环的,当发现一个状态已经出现过,则表明之前的那些状态构成一个循环,同时也是所有状态的集合,所以不可能得到目标状态。复杂度计算:1000(测试组数)*200(2n)*log200(map判重)*200(字符串长度) = 4e7 * log200, 跟方法一差不了多少(log200很小了)。



    代码一:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 500+5;
    19 
    20 char a[MAXN], b[MAXN], c[MAXN], aim[MAXN];
    21 
    22 int main()
    23 {
    24     int T, n;
    25     scanf("%d",&T);
    26     for(int kase = 1; kase<=T; kase++)
    27     {
    28         scanf("%d",&n);
    29         scanf("%s%s%s", a, b, aim);
    30         int ans = -1;
    31         for(int k = 1; k<=2*n; k++)
    32         {
    33             int cnt = 0;
    34             for(int i = 0; i<n; i++)
    35             {
    36                 c[cnt++] = b[i];
    37                 c[cnt++] = a[i];
    38             }
    39             c[cnt] = '';
    40             if(strcmp(c, aim)==0)
    41             {
    42                 ans = k;
    43                 break;
    44             }
    45             for(int i = 0; i<n; i++)
    46             {
    47                 a[i] = c[i];
    48                 b[i] = c[n+i];
    49             }
    50         }
    51         printf("%d %d
    ",kase, ans);
    52     }
    53 }
    View Code


    代码二:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 500+5;
    19 
    20 char a[MAXN], b[MAXN], c[MAXN], aim[MAXN];
    21 map<string, bool>exist;
    22 
    23 int main()
    24 {
    25     int T, n;
    26     scanf("%d",&T);
    27     for(int kase = 1; kase<=T; kase++)
    28     {
    29         scanf("%d",&n);
    30         scanf("%s%s%s", a, b, aim);
    31         exist.clear();
    32         int ans;
    33         for(int k = 1; true; k++)
    34         {
    35             int cnt = 0;
    36             for(int i = 0; i<n; i++)
    37             {
    38                 c[cnt++] = b[i];
    39                 c[cnt++] = a[i];
    40             }
    41             c[cnt] = '';
    42             if(strcmp(c, aim)==0)
    43             {
    44                 ans = k;
    45                 break;
    46             }
    47             else if(exist[c])
    48             {
    49                 ans = -1;
    50                 break;
    51             }
    52             exist[c] = true;
    53             for(int i = 0; i<n; i++)
    54             {
    55                 a[i] = c[i];
    56                 b[i] = c[n+i];
    57             }
    58         }
    59         printf("%d %d
    ",kase, ans);
    60     }
    61 }
    View Code


  • 相关阅读:
    xml根据属性去重。如csprj去重
    table中td的内容换行。
    基于jq的表单填充
    c#包含类文件到csprj中
    t4 根据表名数组生成实体
    js中找string中重复项最多的字符个数
    一步步配置cordova android开发环境
    .net framework卸载工具
    Sql Server查询视图和表
    DbHelper.ttinclude 更新,查询视图和表
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538580.html
Copyright © 2011-2022 走看看