题意
给你一串数字字符串,让你把他分开,分成 n 个数字(也就是 1 - n 都有),n <= 50 。
思路
首先,我们根据字符串的长度可以确定 n 的值:
都是由一位数( 1 - 9 )组成的串,长度即为 n 值;
如果包含两位数, n = ( 长度 - 9 ) / 2 + 9 ;
然后,我们就 dfs 对串进行分割,每次可以割一个数字或者割两个数字(前提是这个割出来的数之前没有没割过(这里可以标记一下),同时不是 0 ,也不是 0 开头的,还不大于 50 )
代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131
#define INF 0x3f3f3f3f
#define P pair<int,int>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps=1e-7;
const double pai=acos(-1.0);
const int N=2e4+10;
const int maxn=4e5+10;
const int mod=1e9+7;
char s[110];
bool vis[110];
int n,len,ans[110],cnt,flag;
void dfs(int pos,int sum)
{
if(sum==n+1)
{
flag=1;
return ;
}
int tmp;
if(pos>len||flag||s[pos]=='0') return ;
tmp=s[pos]-'0';
if(!vis[tmp])
{
ans[cnt++]=tmp;
vis[tmp]=1;
dfs(pos+1,sum+1);
vis[tmp]=0;
if(!flag) cnt--;
}
if(pos+1>len||flag) return ;
tmp=(s[pos]-'0')*10+s[pos+1]-'0';
if(tmp>50) return ;
if(!vis[tmp])
{
ans[cnt++]=tmp;
vis[tmp]=1;
dfs(pos+2,sum+1);
vis[tmp]=0;
if(!flag) cnt--;
}
}
int main()
{
//ios::sync_with_stdio(false);
scanf("%s",s+1);
len=strlen(s+1);
if(len<=9) n=len;
else n=(len-9)/2+9;
dfs(1,1);
for(int i=0;i<cnt-1;i++) printf("%d ",ans[i]);
printf("%d\n",ans[cnt-1]);
return 0;
}