题目传送门

 /*
题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子
DFS:直接模拟搜索,用map记录该字符串是否被搜过。读懂题目是关键。
*/
/************************************************
Author :Running_Time
Created Time :2015-8-3 13:57:55
File Name :POJ_3087.cpp
*************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 5e3 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
map<string, int> cnt;
string str;
bool vis[MAXN][MAXN];
int res, ct;
int n; void DFS(string s, string t, int dep) {
if (!cnt.count (s)) cnt[s] = ++ct;
if (!cnt.count (t)) cnt[t] = ++ct;
if (vis[cnt[s]][cnt[t]]) return ;
vis[cnt[s]][cnt[t]] = true;
string tmp = "";
for (int i=; i<n; ++i) {
tmp += t[i]; tmp += s[i];
}
if (tmp == str) {
if (res > dep) res = dep;
return ;
}
s = ""; t = "";
for (int i=; i<n; ++i) s += tmp[i];
for (int i=n; i<*n; ++i) t += tmp[i];
DFS (s, t, dep + );
} int main(void) { //POJ 3087 Shuffle'm Up
int T, cas = ; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
string s, t;
cin >> s >> t; cin >> str;
res = INF; ct = ; cnt.clear (); memset (vis, false, sizeof (vis));
DFS (s, t, );
printf ("%d %d\n", ++cas, res == INF ? - : res);
} return ;
}

DFS POJ 3087 Shuffle'm Up的更多相关文章

  1. POJ 3087 Shuffle'm Up(洗牌)

    POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 A common pas ...

  2. POJ.3087 Shuffle'm Up (模拟)

    POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...

  3. POJ 3087 Shuffle'm Up

    Shuffle'm Up Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  4. POJ 3087 Shuffle'm Up DFS

    link:http://poj.org/problem?id=3087 题意:给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以 ...

  5. 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 ...

  6. poj 3087 Shuffle'm Up ( map 模拟 )

    题目:http://poj.org/problem?id=3087 题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s ...

  7. POJ 3087 Shuffle'm Up bfs

    题目链接:Shuffle'm Up 除了英文题有点恶心.发现模拟 + bfs 就可以过的时候,就是水了. 一个bug 就是filp函数得到string s12失败了.恩.据大腿告知,string 并不 ...

  8. POJ 3087 Shuffle'm Up (模拟+map)

    题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...

  9. [暴力搜索] POJ 3087 Shuffle'm Up

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Des ...

随机推荐

  1. 关于PHP include文件时的文件查找顺序

    常常被include文件的路径搞晕. 看来是要理一理的时候了. PHP官方文档关于include搜索路径的解释是:先查找工作目录下相对于include_path设置所对应的路径,然后再搜索执行文件所在 ...

  2. codevs1128 导弹拦截

    题目描述 Description 经过11 年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截.当工作半径为0 时,则能够拦截与它位置恰好相同的导弹 ...

  3. msp430入门学习21

    msp430的USART的SPI模式 msp430入门学习

  4. Thinkphp5.0 的响应方式

    Thinkphp5.0 的响应方式 $res = config('default_return_type'); dump($res);//默认是html //修改为json \think\Config ...

  5. python 安装依赖几个问题---HttpScan

    https://blog.csdn.net/chenggong2dm/article/details/61923420 https://www.cnblogs.com/caochuangui/p/59 ...

  6. 创建简单的spring-mvc项目

    1.第一步:创建项目 new—>Dynamic Web Project 项目创建成功后,展示如图: 2.第二步:导入springmvc的jar包和common-logging.jar 3.第三步 ...

  7. 洛谷 P1065 作业调度方案

    P1065 作业调度方案 题目描述 我们现在要利用 mm 台机器加工 nn 个工件,每个工件都有 mm 道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每 ...

  8. MyBatis实体属性与表的字段不对应的解决方案

    1.通过在查询的SQL语句中定义字段名的别名,让字段名的别名和实体类的属性名一致,这样就可以表的字段名和实体类的属性名一一对应上,这种方式是通过在SQL语句中定义别名来解决字段名和属性名的映射关系的. ...

  9. MongoDB小结07 - update【$pop】

    如果将数组看做队列,可以用$pop方法删除第一个或者最后一个元素 {$pop:{"key":-1}},{$pop:{"key":1}}

  10. [Java Sprint] Spring Configuration Using Java

    There is no applicationContext.xml file. Too much XML Namespaces helped Enter Java Configuration Cre ...