Title Link: 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: Accepted users: 57 |
problem 13341: Special Judge |
Problem description |
Joey had saved a permutation of integers from 1 to n 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 of the numbers.
|
Output |
Write a line to the output file with the restored permutation. Don ' t forget the spaces!
If there is several possible original permutations, write any one of the 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 |
The main idea: to divide a string of complete strings into 1~n number. Fill in the blanks and output them.
Problem-Solving ideas: 1, pay attention to this n can be obtained
2, a number of a number comparison, as long as the number is less than n, there is no guarantee that this number has not been visited OK
See the code.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intN,flag;8 Charch[ the];9 intans[ the];Ten BOOLvis[ the]; One A BOOLDfsintIintk) - { - if(flag==1) the return true; - if(k==n+1) - { - for(intI=1; i<k-1; i++) + { -printf ("%d", Ans[i]); + } Aprintf ("%d\n", ans[k-1]); atflag=1; - return true; - } - if(ch[i]-'0'<=n&&!vis[ch[i]-'0']&&ch[i]-'0'>0) - { -ans[k]=ch[i]-'0'; invis[ch[i]-'0']=1; - if(Dfs (i+1, K +1))return true; tovis[ch[i]-'0']=0; + } - if((ch[i]-'0')*Ten+ch[i+1]-'0'<=n&& (ch[i]-'0')*Ten+ch[i+1]-'0'>9&&!vis[(ch[i]-'0')*Ten+ch[i+1]-'0']) the { *Ans[k]= (ch[i]-'0')*Ten+ch[i+1]-'0'; $vis[(ch[i]-'0')*Ten+ch[i+1]-'0']=1;Panax Notoginseng if(Dfs (i+2, K +1))return true; -vis[(ch[i]-'0')*Ten+ch[i+1]-'0']=0; the } + return false; A } the + intMain () - { $ while(SCANF ("%s", ch)! =EOF) $ { -flag=0; -memset (ans,0,sizeof(ans)); thememset (Vis,0,sizeof(Vis)); - intlen=strlen (CH);Wuyi if(len>9) theN= (len-9)/2+9; - Else Wun=Len; -Dfs0,1);//dfs (); About } $ return 0; -}
Hnu Joke with permutation (Deep search Dfs)