小蒜头又调皮了。这一次,姐姐的实验报告惨遭毒手。
姐姐的实验报告上原本记录着从 1 到 n 的序列,任意两个数字间用空格间隔。但是“坑姐”的蒜头居然把数字间的空格都给删掉了,整个数字序列变成一个长度为 1 到 100 的且首部没有空格的数字串。
现在姐姐已经怒了,蒜头找你写个程序快点把试验数据复原。
输入
输入文件有一行,为一个字符串——被蒜头搞乱的实验数据。
字符串的长度在 1 到 100 之间。
输出
输出共一行,为姐姐的原始测试数据—— 1 到 n 的输出。
任意两个数据之间有一个空格。
如果有多组符合要求的正确解,输出其中任意一组即可。
样例1
输入:
4111109876532
输出:
4 1 11 10 9 8 7 6 5 3 2
题意:给一个数字组成的字符串,输出1~n的数字,中间用空格隔开。
分析:暴力搜索,DFS。
#include <iostream>
#include <string.h>
using namespace std;
char num[105];
int ans[105],leng,ma_x;
bool used[105]={0},flag=true;
void dfs(int cnt,int i)
{
if(i==leng&&flag){
for(int j=0;j<cnt-1;j++)
cout<<ans[j]<<' ';
cout<<ans[cnt-1];
flag=false;
return ;
}
int t=num[i]-'0';
if(used[t]||!t||t>ma_x)
goto loop;
ans[cnt]=t;
used[ans[cnt]]=1;
dfs(cnt+1,i+1);
used[ans[cnt]]=0;
loop:if(i<leng-1){
int t=(num[i]-'0')*10+num[i+1]-'0';
if(used[t]||t>ma_x)
return ;
ans[cnt]=t;
used[ans[cnt]]=1;
dfs(cnt+1,i+2);
used[ans[cnt]]=0;
}
return ;
}
int main()
{
cin>>num;
leng=strlen(num);
if(leng>9)
ma_x=(leng-9)/2+9;
else
ma_x=leng;
dfs(0,0);
return 0;
}