CodeForces - 915C Permute Digits (dfs)

题目链接:http://codeforces.com/problemset/problem/915/C点击打开链接

C. Permute Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

It is allowed to leave a as it is.

Input

The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.

Output

Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.

The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.

Examples
input
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940


有点类似于数位dp的思想 就是用一个book记录他当前这位是否限制

然后dfs找最大

特判两数相同和a位数小于b的情况

#include <bits/stdc++.h>
using namespace std;
long long int  num1[10];
long long int  num2[10];
vector<long long int  > judge;
long long int n,m;
long long int length1=0,length2=0;
long long int len;
int flag;
long long int ans;
void dfs(long long int now,long long int pos,long long int book)
{
	if(flag)
		return;
	if(!len&&now<m)
	{
		flag=1;
		ans=now;
		return ;
	}
	if(book)
	{
		for(long long int i=9;i>=0;i--)
		{
			if(num1[i])
			{
				num1[i]--;
				len--;
				long long int mid=pos;
				long long int midnow=now;
				long long int sum=1;
				while(mid--)
					sum*=10;
				sum*=i;
				dfs(now+sum,pos-1,book);
				num1[i]++;
				len++;
			}
		}
	}
	else
	{

		for(long long int i=judge[pos];i>=0;i--)
		{
			if(num1[i])
			{
				if(i==judge[pos])
				{
					num1[i]--;
					len--;
					long long int mid=pos;
					long long int midnow=now;
					long long int sum=1;
					while(mid--)
						sum*=10;
					sum*=i;
					dfs(now+sum,pos-1,0);
					num1[i]++;
					len++;
				}
				else
				{
					num1[i]--;
					len--;
					long long int mid=pos;
					long long int midnow=now;
					long long int sum=1;
					while(mid--)
						sum*=10;
					sum*=i;
					dfs(now+sum,pos-1,1);
					num1[i]++;
					len++;
				}
			}
		}
	}
}
int main()
{
	cin >>n >>m;
	long long int mid=n;
	while(mid)
	{

		num1[mid%10]++;
		mid/=10;
		length1++;
	}
	mid=m;
	while(mid)
	{
		judge.push_back(mid%10);
		num2[mid%10]++;
		mid/=10;
		length2++;
	}
	if(length1<length2)
	{
		for(int i=9;i>=0;i--)
		{
			while(num1[i])
			{
				cout <<i;
				num1[i]--;
			}
		}
		return 0;
	}
	len=length1;
	int fflag=0;
	for(int i=0;i<=9;i++)
		if(num1[i]!=num2[i])
			fflag=1;
	if(fflag)
	{
		dfs(0,length1-1,0);
		cout <<ans <<endl;
	}
	else
	{
		reverse(judge.begin(),judge.end());
		int ffflag=0;
		for(int i=0;i<judge.size();i++)
		{
			if(judge[i]==0&&!ffflag)
				continue;
			else
			{
				cout <<judge[i];
				ffflag=1;
			}
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值