zoukankan      html  css  js  c++  java
  • poj3087 Shuffle'm Up 骗子,这根本不是宽搜,这就是模拟

    本题就直接模拟就OK啦,停止有2个条件,一个条件是达到目的串,另一个条件就是S1经过多次模拟又回到原始串。

    题意不太好懂。那我就说一下吧。

    就是经过shuffle,split,两个步骤,不断的进行这两个步骤,知道出现可以触发停止的条件位置。

     1 #include <iostream>
     2 #include <string>
     3 #include <string.h>
     4 #include <stdio.h>
     5 using namespace std;
     6 int l;
     7 void split(string &t1,string &t2,string t)
     8 {
     9     int i,j;
    10     for(i=0;i<l;++i)
    11     {
    12         t1[i]=t[i];
    13     }
    14     for(i=0,j=l;i<l;++i,++j)
    15     {
    16         t2[i]=t[j];
    17     }
    18     return ;
    19 }
    20 void shuffle(string t1,string t2,string &t)
    21 {
    22     int i,j;
    23     for(i=0,j=0;i<2*l;i=i+2,j++)
    24         t[i]=t2[j];
    25     for(i=1,j=0;i<2*l;i=i+2,j++)
    26         t[i]=t1[j];
    27     return ;
    28 }
    29 int main()
    30 {
    31     int tt;
    32     int k;
    33     int cnt;
    34     string aim,bt1,bt2;
    35     string t1,t2,t;
    36     bool success;
    37     bool sign;
    38     while(cin>>tt)
    39     {
    40         for(k=1;k<=tt;++k)
    41         {
    42             cnt=0;
    43             cin>>l;
    44             cin>>bt1>>bt2>>aim;
    45             success=0;
    46             t1=bt1;
    47             t2=bt2;
    48             t=bt1+bt2;
    49             sign=0;
    50             while(1)
    51             {
    52                 cnt++;
    53                 if(t1==bt1&&sign==1)
    54                     break;
    55                 sign=1;
    56                 shuffle(t1,t2,t);
    57                 if(t==aim)
    58                 {
    59                     success=1;
    60                     break;
    61                 }
    62                 split(t1,t2,t);
    63 
    64             }
    65             cout<<k<<" ";
    66             if(success==0)
    67                 cout<<-1<<endl;
    68             else
    69                 cout<<cnt<<endl;
    70         }
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    javascript中replace()
    防止IE6出现BUG的十种常见解决方法
    IE6 重复字符的bug
    IE6 BUG大全
    display:inline
    JavaScript 图片上传预览效果
    用一行代码让w3wp进程崩溃,如何查找w3wp进程崩溃的原因
    近期学习任务
    气死我的存储过程和用户定义函数
    Damn,China Mobile!!!!
  • 原文地址:https://www.cnblogs.com/symons1992/p/2971927.html
Copyright © 2011-2022 走看看