码迷,mamicode.com
首页 > 其他好文 > 详细

HNU Joke with permutation (深搜dfs)

时间:2015-08-02 11:35:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=13341&courseid=0

Joke with permutation
Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:65536KB
Total submit users: 85, Accepted users: 57
Problem 13341 : Special judge
Problem description

Joey had saved a permutation of integers from 1 to n in a text file. All the numbers were written as decimal numbers without leading spaces.

Then Joe made a practical joke on her: he removed all the spaces in the file.

Help Joey to restore the original permutation after the Joe’s joke!



Input

The input file contains a single line with a single string — the Joey’s permutation without spaces.

The Joey’s permutation had at least 1 and at most 50 numbers.



Output

Write a line to the output file with the restored permutation. Don’t forget the spaces!

If there are several possible original permutations, write any one of them.



Sample Input
4111109876532
Sample Output
4 1 11 10 9 8 7 6 5 3 2
Problem Source
NEERC 2014

Submit   Discuss   Judge Status  Problems  Ranklist 

题目大意:将一串完整的字符串分成1~n个数。将空格补进去,并将其输出。

解题思路:1、注意这个n是可以求出来的

     2、一个数字一个数字比较,只要满足这个数字小于n,还有保证这个数字没有访问过就ok啦

   

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 int n,flag;
 8 char ch[110];
 9 int ans[110];
10 bool vis[110];
11 
12 bool dfs(int i,int k)
13 {
14     if (flag==1)
15         return true;
16     if (k==n+1)
17     {
18         for (int i=1;i<k-1;i++)
19         {
20             printf ("%d ",ans[i]);
21         }
22         printf ("%d\n",ans[k-1]);
23         flag=1;
24         return true;
25     }
26     if (ch[i]-0<=n&&!vis[ch[i]-0]&&ch[i]-0>0)
27     {
28         ans[k]=ch[i]-0;
29         vis[ch[i]-0]=1;
30         if(dfs(i+1,k+1)) return true;
31         vis[ch[i]-0]=0;
32     }
33     if ((ch[i]-0)*10+ch[i+1]-0<=n&&(ch[i]-0)*10+ch[i+1]-0>9&&!vis[(ch[i]-0)*10+ch[i+1]-0])
34     {
35         ans[k]=(ch[i]-0)*10+ch[i+1]-0;
36         vis[(ch[i]-0)*10+ch[i+1]-0]=1;
37         if(dfs(i+2,k+1)) return true;
38         vis[(ch[i]-0)*10+ch[i+1]-0]=0;
39     }
40     return false;
41 }
42 
43 int main()
44 {
45     while (scanf("%s",ch)!=EOF)
46     {
47         flag=0;
48         memset(ans,0,sizeof(ans));
49         memset(vis,0,sizeof(vis));
50         int len=strlen(ch);
51         if (len>9)
52             n=(len-9)/2+9;
53         else
54             n=len;
55         dfs(0,1);//dfs();
56     }
57     return 0;
58 }

 

HNU Joke with permutation (深搜dfs)

标签:

原文地址:http://www.cnblogs.com/qq-star/p/4695254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!