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 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4640476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值