Shuffle'm Up---poj3087

题目链接

题意:有两个字符串s1,s2;经过交叉问是否得到字符串s,不能输出-1,能就输出交叉的次数

每次重组的串都是s2开始,重新组合时,前面一半是s1,后一半s2;

#include<stdio.h>
#include<vector>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#define N 250
using namespace std;

int main()
{
    int T, t=1, n, ans, j;
    scanf("%d", &T);
    while(T--)
    {
        char s1[N]={0}, s2[N]={0}, s[N]={0}, str[N]={0};//记得初始化;
        map<string,int>maps;//个人理解为字符串的值;
        int flag = 0;
        scanf("%d", &n);
        scanf("%s%s%s", s1, s2, s);
        ans =  0;
        while(1)
        {
            ans++;
            j=0;
            for(int i=0; i<n; i++)
            {
                str[j++] = s2[i];
                str[j++] = s1[i];
            }
            s[j]=‘\0‘;
            if(strcmp(str, s)==0)break;
            if(maps[str]==1){flag=1;break;}//导致循环;
            maps[str]=1;//存在过;

            strncpy(s1, str, n);
            strncpy(s2,str+n, n);//更新s1,s2;
        }
        if(flag==1)
            printf("%d -1\n", t++);
        else
            printf("%d %d\n", t++, ans);
    }
    return 0;
}

Shuffle'm Up---poj3087

时间: 2024-11-10 00:52:38

Shuffle'm Up---poj3087的相关文章

POJ3087:Shuffle&#39;m Up(模拟)

http://poj.org/problem?id=3087 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 stac

poj3087 Shuffle&#39;m Up(模拟)

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

POJ3087 Shuffle&#39;m Up 简单模拟

题意:就是给你两副扑克,然后一张盖一张洗牌,不断重复这个过程,看能不能达到目标的扑克顺序 分析:然后就模拟下,-1的情况就是有循环节 #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<map> #include<queue> #include<stdlib.h> #

poj3087 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

【POJ3087】Shuffle&#39;m Up

本题传送门 本题知识点:宽度优先搜索 模拟 + map 本题题意有点懵.就是单纯的把S1像例子那样插到S2里,根本不是什么宽搜题,因为只是一个方向就可以了.说是搜索题倒是有点意思,因为要查重. 不过cin还是慢啊,一个cin就把我卡tle了. 所以最后这就是简单的模拟题,加上一个map(竟然不超时!). 数据很小. #include<iostream> #include<cstdio> #include<cstring> #include<map> usin

搜索专题小结及例题:POJ2251&amp;POJ1426&amp;POJ3087&amp;POJ2488

图的遍历也称为搜索,就是从图中某个顶点出发,沿着一些边遍历图中所有的顶点,且每个顶点仅被访问一次,遍历可采取两种不同的方式:深度优先搜索(DFS)和广度优先搜索(BFS). 1.DFS算法思想` 从顶点v出发深度遍历图G的算法 ① 访问v0顶点,置vis[v0]=1,搜索v0未被访问的邻接点w,若存在邻接点w,则dfs(w),直到到达所有邻接点都被访问过的顶点u为止,接着退回一步,看是否还有其他没有被访问的邻接点.如果有,则访问此顶点,进行前述类似的访问,如果没有,就在退回一步进行搜索,重复上述

[转]完美洗牌(Perfect Shuffle)问题

[转]原博文地址:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/02.09.md 完美洗牌算法 题目详情 有个长度为2n的数组{a1,a2,a3,...,an,b1,b2,b3,...,bn},希望排序后{a1,b1,a2,b2,....,an,bn},请考虑有无时间复杂度o(n),空间复杂度0(1)的解法. 题目来源:此题是去年2013年UC的校招笔试题,看似简单,按照题目所要

【Leetcode】Shuffle an Array

题目链接:https://leetcode.com/problems/shuffle-an-array/ 题目: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and retur

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

Hadoop 1.x的Shuffle源码分析之3

shuffle有两种,一种是在内存存储数据,另一种是在本地文件存储数据,两者几乎一致. 以本地文件进行shuffle的过程为例: mapOutput = shuffleToDisk(mapOutputLoc, input, filename, compressedLength) shuffleToDisk函数如下: private MapOutput shuffleToDisk(MapOutputLocation mapOutputLoc, InputStream input, Path fil