HNU Joke with permutation (深搜dfs)
来源:互联网 发布:js验证提示信息 编辑:程序博客网 时间:2024/06/04 18:20
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
4111109876532Sample Output
4 1 11 10 9 8 7 6 5 3 2Problem SourceNEERC 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 else54 n=len;55 dfs(0,1);//dfs();56 }57 return 0;58 }
- HNU Joke with permutation (深搜dfs)
- Joke with permutation
- Joke with permutation
- Joke with permutation 分类: ACM 2015-08-03 14:09 1
- POJ2168 Joke with Turtles
- CFgym:Digits Permutation(DFS)
- DFS --- HNU 13307 Galaxy collision
- poj 2168 Joke with Turtles
- POJ 2168 Joke with Turtles
- POJ 2168 Joke with Turtles
- Hnu 10104 病毒 (AC自动机+dfs)
- Joke
- joke
- JOKE
- joke
- joke
- joke
- Joke
- hdu 5326 Work(杭电多校赛第三场)
- su 与su -
- hdu 5319 Painter(杭电多校赛第三场)
- hdu 5328 Problem Killer(杭电多校赛第四场)
- hdu 1151 Air Raid(最小路径覆盖)
- HNU Joke with permutation (深搜dfs)
- hdu 1498 50 years, 50 colors(二分匹配_匈牙利算法)
- hdu 2119 Matrix(二分匹配)
- hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)
- 什么是REST?以及RESTful的实现
- hdu 5373 The shortest problem(杭电多校赛第七场)
- ACE_Reactor(二)ACE_Dev_Poll_Reactor
- hdu 2063 过山车 二分匹配(匈牙利算法)
- BNUOJ 12756 Social Holidaying(二分匹配)