DFS POJ 3087 Shuffle'm Up
/*
题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子
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的更多相关文章
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- POJ.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3087 Shuffle'm Up DFS
link:http://poj.org/problem?id=3087 题意:给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以 ...
- 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 ...
- poj 3087 Shuffle'm Up ( map 模拟 )
题目:http://poj.org/problem?id=3087 题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s ...
- POJ 3087 Shuffle'm Up bfs
题目链接:Shuffle'm Up 除了英文题有点恶心.发现模拟 + bfs 就可以过的时候,就是水了. 一个bug 就是filp函数得到string s12失败了.恩.据大腿告知,string 并不 ...
- POJ 3087 Shuffle'm Up (模拟+map)
题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...
- [暴力搜索] POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10003 Accepted: 4631 Des ...
随机推荐
- codevs 1378 选课
题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...
- js格式化日期时间
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).周(E).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...
- FJNUOJ1158(莫比乌斯反演)
题目:给定n个数字a1...an.有m个询问,格式为L R X Y,意为求aL到aR之间与x的最大公因数为y的个数. 数据组数T<=20 1<=n,m<=1e5 1<=ai&l ...
- hihocoder1496(高维前缀和)
题意:给定N个数A1, A2, A3, ... AN,小Ho想从中找到两个数Ai和Aj(i ≠ j)使得乘积Ai × Aj × (Ai AND Aj)最大.其中AND是按位与操作. 第一行一个整数N( ...
- 洛谷 U41571 Agent2
U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...
- Linux系统备份还原工具2(TAR/压缩工具)
相比DD备份还原工具,TAR压缩还原工具更加小巧和灵活,但是不能备份MBR.当然可以通过重新安装GRUB来解决MBR的这一问题.同时,TAR的做法也是官方推荐的. 注意:一个硬盘启动时最新经过MBR( ...
- json序列化后的是字符串,不是二进制。是字符串!!!确定不是二进制!!!
1.现有一个自定义对象需要储存到sql数据库中去.这个对象里面属性很多,甚至包含一些元素量打到几十万的List集合属性.本人试着使用JSON序列化这个对象,储存到数据库,报maxjsonlength超 ...
- Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)
原文:https://my.oschina.net/xshuai/blog/917097 摘要: Nginx+Keepalived搭建高可用负载均衡环境(HA) http://blog.csdn.ne ...
- Xcode iOS9.3 配置包 iOS10.0 配置包 iOS10.2 配置包 could not find developer disk image
在Finder状态下前往目录.快捷键:shift+command+G,填写路径/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS ...
- 连接App.config
ConfigurationManager.AppSettings["AdminName"]; 连接App.config的字符