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