Problem J. Jokewithpermutation
Input file: joke.in
Output file: joke.out
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 and output
joke.in joke.out
4111109876532 4 1 11 10 9 8 7 6 5 3 2
这是区域赛的题目!!!!
DFS 暴力搜索 看了yan的代码 自己理解了一下
注意几点 1.vetor的使用 姿势
2.字符串长度判断最大n
3.一位或二位字符组成一个数
4.dfs姿势要熟练!!!!!
#include<bits/stdc++.h> using namespace std; map<int,int>mp; vector<int>re; int s=0; int po; char a[105]; int len; void dfs(int n) { if(n==len) { int flag=1; for(int i=1;i<=s;i++) { if(!mp[i]) flag=0; } //cout<<re.size()<<endl; if(flag) { for(unsigned int i=0;i<re.size()-1;i++) printf("%d ",re[i]); printf("%d ",re[re.size()-1]); po=1; } return ; } if(po) return ; if(!mp[a[n]-'0']) { mp[a[n]-'0']=1; re.push_back(a[n]-'0'); dfs(n+1); if(po) return ; re.pop_back(); mp[a[n]-'0']=0; } if(!mp[(a[n]-'0')*10+a[n+1]-'0']&&((a[n]-'0')*10+a[n+1]-'0')<=s&&po==0&&n+1<len) { mp[(a[n]-'0')*10+a[n+1]-'0']=1; re.push_back((a[n]-'0')*10+a[n+1]-'0'); dfs(n+2); if(po) return; mp[(a[n]-'0')*10+a[n+1]-'0']=0; re.pop_back(); } } int main() { freopen("joke.in","r",stdin); freopen("joke.out","w",stdout); gets(a); len=strlen(a); re.clear(); mp.clear(); if(len<=9) { for(int i=0; i<len-1; i++) printf("%c ",a[i]); printf("%c ",a[len-1]); return 0; } po=0; s=(len-9)/2+9; dfs(0); return 0; }