牛客-Jokewithpermutation(dfs)

在这里插入图片描述

题意

给你一串数字字符串,让你把他分开,分成 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值