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;
    }
  • 相关阅读:
    京东二面面经(07.17 11:30)
    招银三面手撕代码题(字符串连续子串)
    shein二面(31min)
    京东提前批一面
    两个链表的第一个公共结点
    Java并发机制的底层实现原理
    招银网络(二面07.09)
    黑盒测试与白盒测试
    求1+2+...+n(剑指offer-47)
    第一个只出现一次的字符(剑指offer-34)
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/6086770.html
Copyright © 2011-2022 走看看