Jokewithpermutation (DFS)

时间:2023-11-11 16:33:08

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=;
int po;
char a[];
int len;
void dfs(int n)
{
if(n==len)
{
int flag=;
for(int i=;i<=s;i++)
{
if(!mp[i])
flag=;
}
//cout<<re.size()<<endl;
if(flag)
{
for(unsigned int i=;i<re.size()-;i++)
printf("%d ",re[i]);
printf("%d\n",re[re.size()-]);
po=;
}
return ;
}
if(po)
return ;
if(!mp[a[n]-''])
{
mp[a[n]-'']=;
re.push_back(a[n]-'');
dfs(n+);
if(po)
return ;
re.pop_back();
mp[a[n]-'']=;
}
if(!mp[(a[n]-'')*+a[n+]-'']&&((a[n]-'')*+a[n+]-'')<=s&&po==&&n+<len)
{
mp[(a[n]-'')*+a[n+]-'']=;
re.push_back((a[n]-'')*+a[n+]-'');
dfs(n+);
if(po)
return;
mp[(a[n]-'')*+a[n+]-'']=;
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<=)
{
for(int i=; i<len-; i++)
printf("%c ",a[i]);
printf("%c\n",a[len-]);
return ;
}
po=;
s=(len-)/+;
dfs();
return ;
}