[递归 搜索] Beautiful Now HDU6351

Beautiful Now

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1930    Accepted Submission(s): 730


 

Problem Description

Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautiful after k swaps of digits.
Let the decimal representation of n as (x1x2⋯xm)10 satisfying that 1≤x1≤9, 0≤xi≤9 (2≤i≤m), which means n=∑mi=1xi10m−i. In each swap, Anton can select two digits xi and xj (1≤i≤j≤m) and then swap them if the integer after this swap has no leading zero.
Could you please tell him the minimum integer and the maximum integer he can obtain after k swaps?

 

 

Input

The first line contains one integer T, indicating the number of test cases.
Each of the following T lines describes a test case and contains two space-separated integers n and k.
1≤T≤100, 1≤n,k≤109.

 

 

Output

For each test case, print in one line the minimum integer and the maximum integer which are separated by one space.

 

 

Sample Input

 

5

12 1

213 2

998244353 1

998244353 2

998244353 3

 

 

Sample Output

 

12 21

123 321

298944353 998544323

238944359 998544332

233944859 998544332

 

 

Source

2018 Multi-University Training Contest 5

 

 

 

#include <bits/stdc++.h>
using namespace std;

int a[20], b[20];
int len;
int Min, Max;
void dfs1(int s, int t, int k)
{
	if (k == 0)
	{
		int sum = 0;
		for (int i = 1; i <= len; i++)
			sum = sum * 10 + a[i];
		Min = min(Min, sum);
		return;
	}
	
	for (int i = s; i <= len; i++)
	{
		int t = a[i];
		for (int j = i; j <= len; j++)
		{
			if (i == 1 && a[j] == 0)
				continue;
			if (t > a[j])
				t = a[j];
		}  /// 从当前位起往后最小的值
		for (int j = i; j <= len; j++)
		{
			if (a[j] == t)
			{
				swap(a[i], a[j]); /// 交换一个满足条件的j
				dfs1(i + 1, len, k - 1);  /// 从i + 1开始到len交换k - 1次
				swap(a[i], a[j]); /// 还原 交换下一个满足条件的j
			}
		}
	}
}

void dfs2(int s, int t, int k)
{
	if (k == 0)
	{
		int sum = 0;
		for (int i = 1; i <= len; i++)
			sum = sum * 10 + b[i];   
		Max = max(Max, sum);
		return;
	}
	
	for (int i = s; i <= len; i++)
	{
		int t = b[i];
		for (int j = i; j <= len; j++)
		{
			if (t < b[j])
				t = b[j];
		}
		for (int j = i; j <= len; j++)
		{
			if (b[j] == t)
			{
				swap(b[i], b[j]);
				dfs2(i + 1, len, k - 1);
				swap(b[i], b[j]);
			}
		}
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("D:\\in.txt", "r", stdin);
	#endif // ONLINE_JUDGE
	
	int T;
	scanf("%d", &T);
	while (T--)
	{
		char ch[20]; int k;
		scanf("%s %d", ch + 1, &k);
		len = strlen(ch + 1);
		
		for (int i = 1; i <= len; i++)
		{
			a[i] = ch[i] - '0';
			b[i] = a[i];
		}
		
		/// 数组长度为len, 最多交换len - 1次可得最值
		/// 如 len = 9, 234567891 交换8次得最小值123456789
		if (k >= len - 1)
		{
			sort(a + 1, a + len + 1);
			sort(b + 1, b + len + 1);
			
			int t = 1; 
			while (a[t] == 0)
				t++;
			swap(a[t], a[1]);
			for (int i = 1; i <= len; i++)
				printf("%d", a[i]);
			printf(" ");
			
			for (int i = len; i >= 1; i--)
				printf("%d", b[i]);
			printf("\n");
			
			continue;
		}
		
		Min = 0x7fffffff, Max = 0;
		dfs1(1, len, k);  /// 从1到len交换k次
		dfs2(1, len, k);
		printf("%d %d\n", Min, Max);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
递归搜索FTP目录下的子目录可以使用VC编程语言来实现。 首先,我们需要使用VC中的FTP类库来连接到FTP服务器并获取目录列表。可以使用CInternetSession对象来建立连接,并使用CFtpConnection对象来访问FTP服务器。 接下来,我们可以使用CFtpConnection的FindFile函数来遍历当前目录下的文件和子目录。可以使用FindNextFile函数来获取文件和目录的名称和属性。 为了递归搜索子目录,我们可以使用一个自定义的递归函数。在这个函数中,我们可以检查每个找到的子目录,如果存在子目录,则进一步调用递归函数来搜索下一级目录。 递归函数的基本步骤如下: 1. 定义递归函数,传入当前目录的路径和FTP连接对象作为参数。 2. 使用FindFile函数开始搜索当前目录下的文件和子目录。 3. 使用FindNextFile函数遍历找到的文件和子目录。 4. 对于每个找到的子目录,检查是否为目录类型,如果是,则调用递归函数来搜索该子目录。 5. 递归函数中的搜索过程会不断重复,直到找到没有子目录的最底层目录,然后返回上一级目录。 6. 最终得到所有子目录的搜索结果。 需要注意的是,递归搜索在处理大量文件时可能会导致性能问题。可以考虑使用多线程来提高搜索效率,或者使用迭代方法来避免递归深度过大的问题。 总之,使用VC编程语言可以通过递归搜索函数来实现FTP目录下子目录的搜索,这可以帮助我们在FTP服务器上查找特定的文件或目录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值