Total replacement of garlic off joke (DFS)

 

 

Portal

Subject to the effect:

Garlic and a little naughty. This time, the test report her sister murdered in cold blood.

Originally recorded sequence from 1 to n on the lab sister, between any two numbers separated by spaces. However, "pit sister" garlic actually spaces between the digital gave deleted, the entire length of the digital sequence into a string of digits and no spaces header is 1 to 100.

Now my sister has anger, garlic find you quickly write a program to test data recovery.

Entry

Input file a line, a string - is the experimental data garlic messing.

String length between 1 and 100.

Export

The output common line, to test raw sister Data - 1 to n outputs.

There is a space between any two data.

If more than one set in line with the requirements of the correct solution, output any of a group can be.

The only answer to this question does not meet the requirements of the answers are correct

Sample input

4111109876532

Sample Output

4 1 11 10 9 8 7 6 5 3 2

 

Problem-solving ideas:

Because the length of the string does not exceed 100, we can calculate the maximum is 54 n, we only need to enumerate, and a two-digit number.

First calculate the length of the string through n, labeled enumerator over an array of open number, not only the number of labeled and smaller than or equal to n continue to the next search will survive.

 

size<=9: n=size

size>9:  n=(size-9)/2+9

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const double PI = acos(-1);
17 const double eps =1e-8;
18 #define Bug cout<<"---------------------"<<endl
19 const int maxn=1e5+10;
20 using namespace std;
21 const int prime[15]={2,3,5,7,11,13,17,. 19 , 23 is , 29 , 31 is , 37 [ , 41 is , 43 is , 47 };
 22 is  
23 is  String STR;
 24  int n-; // advance calculated, can be used to prune 
25  int ANS [ 105 ]; 
 26 is  BOOL VIS [ 105 ]; // determine if a number appears not 
27  BOOL Flag;
 28  
29  void the DFS ( int the STEP, int NUM) // the STEP represents several characters, num said it had found a number NUM 
30 {
31     if(flag) return;//最优性剪枝 
32     if(step==str.size())
33     {
34         for(int i=0;i<num;i++)
35         {
36             if(i==0) cout<<ans[i];
37             else cout<<' '<<ans[i];
38         }
39         cout<<endl;
40         flag=true;
41         return ;
42     }
43     int t=str[step]-'0';
44     if(t<=n&&!vis[t])//搜一位 
45     {
46         ans[num]=t;
47         vis[t]=true;
48         DFS(step+1,num+1);
49         vis[t]=false;
50     }
51     if(step!=str.size()-1)
52     {
53         t=t*10+str[step+1]-'0';
54         if(t<=n&&!vis[t])//搜两位 
55         {
56             ans[num]=t;
57             vis[t]=true;
58             DFS(step+2,num+1);
59             vis[t]=false;
60         }
61     }
62 }
63 
64 int main()
65 {
66     #ifdef DEBUG
67     freopen("sample.txt","r",stdin);
68     #endif
69 //    ios_base::sync_with_stdio(false);
70 //    cin.tie(NULL);
71     
72     cin>>str;
73     n=str.size()<=9?str.size():(str.size()-9)/2+9;
74     DFS(0,0);
75     
76     return 0;
77 }

 

 

 

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12178818.html