zoukankan      html  css  js  c++  java
  • POJ 3087 Shuffle'm Up

      赤裸裸的模拟题。。

      给出字符串 s1,s2,s12;判断s1,s2能够通过题目中所给的规则到达 s12。每次只会产生一种新的状态,还BFS个毛线--

      s1的首位新生成的字符串的首,s2的尾为新生成字符串的尾。中间的字符依次交叉排列。

      再将新生成的字符串的前一半给s1,后一半给s2。

      重复上述两步 直到到达 s12 或者 新生成的字符串已经出现过。

      前者输出步数,后者输出 -1。

      

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 
     7 using namespace std;
     8 
     9 struct N
    10 {
    11     char s[210];
    12     N *l,*r;
    13 };
    14 
    15 N *creat()
    16 {
    17     N *p = (N *)malloc(sizeof(N));
    18     p->l = p->r = NULL;
    19     return p;
    20 }
    21 
    22 bool check(char *s,N *&root)
    23 {
    24     if(root == NULL)
    25     {
    26 
    27         root = creat();
    28         strcpy(root->s,s);
    29 
    30         return true;
    31     }
    32     int order = strcmp(s,root->s);
    33 
    34     if(order == 0)
    35         return false;
    36 
    37     if(order < 0)
    38         return check(s,root->l);
    39 
    40     return check(s,root->r);
    41 }
    42 
    43 int main()
    44 {
    45     int T,icase = 0,ans;
    46 
    47     scanf("%d",&T);
    48 
    49     while(T--)
    50     {
    51         int len,i;
    52 
    53         char s1[110],s2[110],s12[210],t[210];
    54 
    55         N *root = NULL;
    56         ans = 0;
    57 
    58         scanf("%d%*c",&len);
    59 
    60         scanf("%s%s%s",s1,s2,s12);
    61 
    62         strcpy(t,s12);
    63 
    64         while(check(t,root))
    65         {
    66             ++ans;
    67             for(i = 0;i < len; ++i)
    68             {
    69                 t[2*i] = s2[i];
    70                 t[2*i+1] = s1[i];
    71             }
    72             t[2*len] = '';
    73             for(i = 0;i < len; ++i)
    74             {
    75                 s2[i] = t[i+len];
    76                 s1[i] = t[i];
    77             }
    78             s1[len] = s2[len] = '';
    79         }
    80         if(strcmp(t,s12) == 0)
    81             printf("%d %d
    ",++icase,ans);
    82         else
    83             printf("%d -1
    ",++icase);
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    css数学运算函数 calc(),和css的数学运算
    MySQL设置字段的默认值为当前系统时间
    今天阿里云服务器被挂马wnTKYg挖矿的清理
    linux shell常用命令
    无损扩容,调整Centos6.5服务器分区大小,不适用centos7,centos6.5 调整逻辑卷大小
    添加远程库
    interface 设置默认值
    radio根据value值动态选中
    获取下拉js 具体值
    mysql中int、bigint、smallint 和 tinyint的存储
  • 原文地址:https://www.cnblogs.com/zmx354/p/3272973.html
Copyright © 2011-2022 走看看