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

    [link:http://poj.org/problem?id=3087](http://poj.org/problem?id=3087) **题意:**给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以把下面的一半作为S1,上面的一半作为S2,问能否洗出题目给出的最终字串。 **思路:**很好能够找到规律,就是先把两串合并,分别存a[i],a[i+n/2]到新串中,这个新串就是当前洗出的结果。因此进行DFS,由于给出的串长为偶数(?)所以必定能够洗回初始状态,所以出口就是初始串。
    /** @Date    : 2016-11-17-22.11
    * @Author : Lweleth (SoungEarlf@gmail.com)
    * @Link : https://github.com/
    * @Version :
    */
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <utility>
    #include <vector>
    #include <map>
    #include <set>
    #include <string>
    #include <stack>
    #include <queue>
    //#include<bits/stdc++.h>
    #define LL long long
    #define MMF(x) memset((x),0,sizeof(x))
    #define MMI(x) memset((x), INF, sizeof(x))
    using namespace std;

    const int INF = 0x3f3f3f3f;
    const int N = 1e5+2000;

    char a[500];
    char b[500];
    char c[500];
    char t[500];


    int n;
    int ans = 0;
    int dfs(char *x, char *y)
    {
    int cnt = 0;
    for(int i = 0; i < n; i++)
    {
    x[cnt++] = y[i+n];
    x[cnt++] = y[i];
    }
    x[cnt] = '';
    ans++;
    if(strcmp(x, b) == 0)
    {
    //printf("!%s ", x);
    return ans;
    }
    if(strcmp(x, t) == 0)
    {
    //printf("~%s ", x);
    return -1;
    }
    dfs(y, x);
    }

    int main()
    {
    int T;
    cin >> T;
    int cnt = 0;
    while(T--)
    {
    ans = 0;
    scanf("%d", &n);
    scanf("%s", a);
    scanf("%s", a + n);
    scanf("%s", b);
    strcpy(t, a);
    printf("%d %d ",++cnt, dfs(c, a));
    }
    return 0;
    }
  • 相关阅读:
    内存映射mmap的几个api及其使用
    hiredis的安装
    Linux 下解压大全
    redis内存数据库C客户端hiredis API 中文说明
    C/C++使用MySQL
    搜索引擎的缓存(cache)机制
    快速排序(QuickSort)
    冒泡排序
    spring核心之AOP学习总结一
    Spring学习总结六——SpringMVC一
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/6086770.html
Copyright © 2011-2022 走看看