DFS Gym 100553J Jokewithpermutation

题目传送门

 1 /*
 2     题意:将字符串分割成一个全排列
 3     DFS:搜索主要在一位数和两位数的处理,用d1, d2记录个数,在不饱和的情况下,两种都试一下
 4         DFS还是写不来,难道是在家里懒?
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cstring>
 9 #include <cmath>
10 using namespace std;
11
12 const int MAXN = 1e2 + 10;
13 const int INF = 0x3f3f3f3f;
14 char s[MAXN];
15 bool vis[55];
16 int ans[55];
17 int len, tot, n1, n2;
18
19 bool DFS(int d1, int d2, int p, int cnt)
20 {
21     if (cnt == tot)    return true;
22     if (d1 < n1)
23     {
24         int x = s[p] - ‘0‘;
25         if (x != 0 && !vis[x])
26         {
27             vis[x] = true;    ans[cnt+1] = x;
28             if (DFS (d1+1, d2, p+1, cnt+1))    return true;
29             vis[x] = false;
30         }
31     }
32     if (d2 < n2)
33     {
34         int x = (s[p] - ‘0‘) * 10 + (s[p+1] - ‘0‘);
35         if (x >= 0 && x <= tot && !vis[x])
36         {
37             vis[x] = true;    ans[cnt+1] = x;
38             if (DFS (d1, d2+1, p+2, cnt+1))    return true;
39             vis[x] = false;
40         }
41     }
42
43     return false;
44 }
45
46 int main(void)        //Gym 100553J Jokewithpermutation
47 {
48 //    freopen ("J.in", "r", stdin);
49     freopen ("joke.in", "r", stdin);
50     freopen ("joke.out", "w", stdout);
51
52     while (scanf ("%s", s + 1) == 1)
53     {
54         memset (vis, false, sizeof (vis));
55         len = strlen (s + 1);
56         tot = (len + 9) / 2;
57         if (tot <= 9)
58         {
59             for (int i=1; i<=len; ++i)
60             {
61                 printf ("%c%c", s[i], (i==len) ? ‘\n‘ : ‘ ‘);
62             }
63         }
64         else
65         {
66             n1 = 9;    n2 = tot - 9;
67             DFS (0, 0, 1, 0);
68             for (int i=1; i<=tot; ++i)
69             {
70                 printf ("%d%c", ans[i], (i==tot) ? ‘\n‘ : ‘ ‘);
71             }
72         }
73     }
74
75     return 0;
76 }
时间: 2024-08-05 08:56:10

DFS Gym 100553J Jokewithpermutation的相关文章

Gym 100553J Jokewithpermutation(dfs)

题意:有n个数(n<=50)写在一行,将两数之间的空格去掉得到一个数字串.根据这个数字串回复原数: 思路:先求数的个数,当串长度小于10时,均为个位数:当串长度大于9时,存在两位数,剪去9个个位数,同样得到数的个数: 数的个数也是串中的最大数: 采用枚举的方法将每个数与串中的位置匹配,属于同一个数的数字对应同一个数: 输出时,两位数的数字间不加空格: #include<cstdio> #include<cstring> #include<algorithm> us

很好的脑洞题:dfs+暴力 Gym - 101128A Promotions

http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次性取出来的个数为a(或b).当然,取出来的种类可能有很多种(即一个集合),问,这个集合中有多少个数字是相同的. 第一个输出集合长度为a的,第二个输出集合长度为b的,第三个输出无论如何都无法被取出的个数. 思路:建立正向图和反向图. 定义pair<int, int> interval[i] 表示第i

Gym 101246D Fire in the Country(dfs求SG函数)

http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从1点出发,每个人每次选择一个点走,谁最后被火烧了谁就输了. 思路: 博弈题. 我们先预处理求出每个点着火的时间点,然后根据时间点重建新图,也就是重新建一个有向无环图,原来图中相连的并且时间点相差1的相连,由时间低的连向时间高的. 接下来我们在新图上求每个点的SG值,SG值为0的点就是叶子结点,这样父

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

Gym 100463D Evil DFS

Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description Richard is evil. He wants to give another geometry problem to the participants of this contest and I’m afraid I have no choice but to comply. Wh

codeforces Gym 100187J J. Deck Shuffling dfs

J. Deck Shuffling Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/J Description The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and k s

Gym 100338I TV show (dfs枚举)

Gym 100338I 题意: 一个人去参加电视有奖问答的节目,初始奖金为100元,每答对一道问题奖金翻倍,答错奖金清零.此外有一次保险机会:花费C的奖金,下一题可以答对奖金翻倍,答错奖金不清零. 现在给你答对每道题的概率,求最优答题策略的奖金期望. 思路: 先不考虑有保险机会.回答对第j题后离开的奖金期望就是: 100?2j?∏ji=1pi 那么我们枚举回答对第j题后离开的奖金期望,维护其最大值即可(注意也可以一题不回答直接走人,期望为100). 那么我们现在来考虑有保险机会的情况,我们枚举回

CodeForces Gym 100500A A. Poetry Challenge DFS

Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Let’s check another challenge of the IBM ICPC Chill Zone, a poetry challenge. One says a poetry string that starts with a

【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs&#39; Box Boutique

给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排序. 如果前面的宽大于等于后面的宽,就连接一条边. 形成一张DAG,拓扑排序后跑最长路即可. #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespa