HDU3283(next_permutation)

本博客探讨了一个在给定数字排列的基础上寻找下一个字典序更大排列的问题,通过编程实现并解释了核心算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Next Permutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 619    Accepted Submission(s): 440


Problem Description
For this problem, you will write a program that takes a (possibly long) string of decimal digits, and outputs the permutation of those decimal digits that has the next larger value (as a decimal number)
than the input number. For example:
123 -> 132
279134399742 -> 279134423799

It is possible that no permutation of the input digits has a larger value. For example, 987.
 

Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by up to 80 decimal digits which is the input value.
 

Output
For each data set there is one line of output. If there is no larger permutation of the input digits, the output should be the data set number followed by a single space, followed by the string BIGGEST. Ifthere is a solution, the output should be the data set number, a single space and the next larger
permutation of the input digits.
 

Sample Input
  
3 1 123 2 279134399742 3 987
 

Sample Output
  
1 132 2 279134423799 3 BIGGEST
 

Source
 
库函数是个有东西!

范围由[first,last)标记,调用next_permutation使数列逐次增大,这个递增过程按照字典序。例如,在字母表中,abcd的下一单词排列为abdc,但是,有一关键点,如何确定这个下一排列为字典序中的next,而不是next->next->next……

若当前调用排列到达最大字典序,比如dcba,就返回false,同时重新设置该排列为最小字典序。

返回为true表示生成下一排列成功

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
char str[100];

int main()
{
	int num_case,ca;
	cin>>num_case;
	while(num_case--)
	{
		scanf("%d %s",&ca,str);
		if(next_permutation(str,str+strlen(str)))
			cout<<ca<<" "<<str<<endl;
		else 
			cout<<ca<<" BIGGEST"<<endl;

	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值