POJ 3087 Shuffle'm Up 线性同余,暴力 难度:2

http://poj.org/problem?id=3087

设:s1={A1,A2,A3,...Ac}

s2={Ac+1,Ac+2,Ac+3,....A2c}

合在一起成为

Ac+1,A1,Ac+2,A2......A2c,Ac

经过一次转换之后变成

s1={Ac+1,A1,Ac+2.....}

s2={...A2c,Ac}

对应之前,每个数的序号发生的变化是

+1,+2,+3....-c,-c+1,.....

把整个数链想成环,也相当于是:

+1,+2,+3....+c,+c+1,.......

例如A1,由A1->A2->A4->A7....c次之后必然回到A1

所以整个串经过一定次数的变换一定会回到最初状态,只需判断在回到最初状态之前有没有得到目标状态即可

#include <cstdio>//a=(a+c+1)%(2*c)
#include <cstring>
using namespace std;
const int maxn=1002;
int c;
char s1[maxn],s2[maxn],aim[maxn],org[maxn],tmp[maxn];
void shuffle(){
        for(int i=0;i<c;i++){
                tmp[2*i]=s2[i];
                tmp[2*i+1]=s1[i];
        }
        tmp[2*c]=0;
}
int main(){
        int T;
        scanf("%d",&T);
        for(int ti=1;ti<=T;ti++){
                scanf("%d%s%s%s",&c,s1,s2,aim);
                shuffle();
                strcpy(org,tmp);
                int ans=1;
                if(strcmp(tmp,aim)==0){
                        printf("%d 1\n",ti);
                        continue;
                }
                bool fl=false;
                while(strcmp(org,tmp)!=0||ans==1){
                        strncpy(s1,tmp,c);
                        strncpy(s2,tmp+c,c);
                        shuffle();
                        ans++;
                        if(strcmp(tmp,aim)==0){
                                fl=true;
                                printf("%d %d\n",ti,ans);
                                break;
                        }
                }
                if(!fl)printf("%d -1\n",ti);
        }
        return 0;
}

  

POJ 3087 Shuffle'm Up 线性同余,暴力 难度:2

时间: 2024-10-05 04:24:22

POJ 3087 Shuffle'm Up 线性同余,暴力 难度:2的相关文章

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

POJ 3087 Shuffle&#39;m Up(模拟退火)

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

POJ 3087 Shuffle&#39;m Up (模拟+map)

题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s1,最顶的c块牌归为s2,依此循环下去. 现在输入s1和s2的初始状态 以及 预想的最终状态s12.问s1 s2经过多少次洗牌之后,最终能达到状态s12,若永远不可能相同,则输出"-1". 解题思路:照着模拟就好了,只是判断是否永远不能达到状态s12需要用map,定义map<

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 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 o

(简单) POJ 3087 Shuffle&#39;m Up,枚举。

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

模拟/poj 3087 Shuffle&#39;m Up

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char s1[110],s2[110],ss[220]; 5 int len,n; 6 7 int f() 8 { 9 int ans=0; 10 char t1[110],t2[110],tt[220]; 11 strcpy(t1,s1);strcpy(t2,s2); 12 while (1) 13 { 14 ans++; 15 for (int i=

POJ 3087 Shuffle&#39;m Up

POJ 3087 Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8843   Accepted: 4078 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

POJ 3087 Shuffle&#39;m Up (模拟)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5850   Accepted: 2744 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

POJ 3087 Shuffle&amp;#39;m Up(模拟)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7404   Accepted: 3421 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