POJ 3087 Shuffle'm Up (深搜)
2018-04-04 19:25
267 查看
【题目链接】
http://poj.org/problem?id=3087
http://poj.org/problem?id=3087
题目意思
模拟洗牌的过程,每次把牌分成两部分。然后下部分的牌一张上部分牌一张,无限循环下去问你是否能切出对应的牌。如果能要洗多少次,如果不能就输出-1(用字符表示各种牌)。解题思路
模拟分牌,合牌过程,由于不断重复这个过程递归就可以了代码部分
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <queue> #include <string> #include <map> using namespace std; #define LL long long #define inf 0x3f3f3f3 const int N = 1e5+5; int a[4]={1000,100,10,1}; int n; string s12,s1,s2; map<string , int>M; int dfs (string s1, string s2,int step) { string c1; ///串1 string c2; ///串2 string c12; ///合串 c12 = s1+s2; if (M[c12] == 1) ///出现过的串说明循环了 return -1; if (c12 == s12) return step; M[c12]++; int k = 0; for (int i = 0; i < n; i++) ///重新分串 { if (k < n) c1 += s2[i]; else c2 += s2[i]; k++; if (k < n) c1 += s1[i]; else c2 += s1[i]; k++; } return dfs(c1,c2,step+1); } int main() { int t,cas = 1; scanf("%d",&t); while (t--) { M.clear(); scanf("%d",&n); cin>>s1>>s2>>s12; int s=dfs(s1,s2,0); printf("%d %d\n",cas++,s); } return 0; }
相关文章推荐
- POJ 3087 Shuffle'm Up
- POJ 3087 Shuffle'm Up(字符串处理,模拟)
- POJ-3087- Shuffle'm Up(字符串模拟)
- POJ 3087 Shuffle'm Up (模拟)
- POJ-3087 (Shuffle'm Up)
- poj 3087 Shuffle'm Up
- poj 3087 Shuffle'm Up 模拟
- Shuffle'm Up poj 3087 bfs
- [模拟]POJ 3087 Shuffle'm up
- poj 3087 Shuffle'm Up
- POJ 3087 Shuffle'm Up(模拟) (G)
- POJ 3087 Shuffle'm Up
- poj 3087 Shuffle'm Up
- Shuffle'm Up POJ - 3087
- POJ 3087 Shuffle'm Up 简单的模拟题
- POJ 3087 Shuffle'm Up——思路题
- [kuangbin带你飞]专题1 简单搜索 G - Shuffle'm Up POJ - 3087
- poj 3087 Shuffle'm Up
- POJ3087--Shuffle'm Up
- POJ 3087 Shuffle'm Up 笔记