The Next Permutation

Time Limit: 2000/1000ms (Java/Others)

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.
译文:对于这个问题,你将编写一个程序,它接受一个(可能很长的)十进制数字串,并输出具有比输入数字更大的值(十进制数)的那些十进制数字的排列。例如:
123 - > 132
279134399742 - > 279134423799
输入数字的排列可能没有更大的值。例如,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.
译文:第一行输入包含一个整数P,(1≤P≤1000),这是后面的数据集的数量。每个数据集都是一行,其中包含数据集编号,后跟一个空格,然后是最多80个十进制数字,即输入值。

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. If there is a solution, the output should be the data set number, a single space and the next larger permutation of the input digits.
译文:对于每个数据集有一行输出。如果输入数字没有大的排列,则输出应该是数据集编号,后跟一个空格,然后是字符串BIGGEST。如果有解决方案,输出应该是数据集编号,一个空格和下一个较大的输入数字排列。

Sample Input:

3
1 123
2 279134399742
3 987

Sample Output:

1 132
2 279134423799
3 BIGGEST
解题思路:80个10进制数字,即80位数,基本数据类型都不能表示,但C++万能的STL中有next_permutation,用它即可解决此题是否有下一个排列。
str.begin()和str.end() 可以快速访问到字符串的首字符和尾字符。如果有下一个排列,next_permutation(str.begin(), str.end())为真值,否则为假值。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
int main()
{
int p,n;string str;
cin>>p;
for(int i=;i<=p;++i){
cin>>n>>str;
cout<<n<<' ';
if(next_permutation(str.begin(),str.end()))cout<<str<<endl;
else cout<<"BIGGEST"<<endl;
}
return ;
}

ACM_下一个排列的更多相关文章

  1. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  2. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  3. C++构造 下一个排列 的函数

    今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...

  4. LinkCode 下一个排列、上一个排列

    http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...

  5. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. [Swift]LeetCode31. 下一个排列 | Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  8. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. leetcode31题:下一个排列

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. ...

随机推荐

  1. bzoj2212 Tree Rotations

    被BZOJ坑了一下午,原以为是我程序有问题一直WA,结果是我数组小了...为啥不给我RE!!! 线段树合并,对于逆序对而言,只能通过交换左右子树来达到,那么我们就可以想到对于一个结点而言,我们当然要取 ...

  2. HDU——3072 Intelligence System

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. 洛谷 P1883 函数

    P1883 函数 题目描述 给定n个二次函数f1(x),f2(x),...,fn(x)(均形如ax^2+bx+c),设F(x)=max{f1(x),f2(x),...,fn(x)},求F(x)在区间[ ...

  4. mysql计算两个日期之间的天数

    MYSQL自带函数计算给定的两个日期的间隔天数   有两个途径可获得   1.利用TO_DAYS函数   select to_days(now()) - to_days('20120512')   2 ...

  5. MySQL架构优化实战系列4:SQL优化步骤与常用管理命令

  6. git fetch 和 git pull 的差别

    Git中从远程的分支获取最新的版本号到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本号到本地,不会自己主动merge git fetch origin master git ...

  7. nlssort排序

    ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码进行排序的. 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NL ...

  8. 硬件十万个为什么——运放篇(五)PCB设计技巧

    1.在PCB设计时,芯片电源处旁路滤波等电容应尽可能的接近器件.典型距离是小于3MM 2.运算放大器芯片电源处的小陶瓷旁路电容在放大器处于输入高频信号时能够为放大器的高频特性提供能量电容值的选择依据输 ...

  9. Python开发【第*篇】【Xpath与lxml类库】

    什么是XML XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 的标签需要 ...

  10. Kernel Live-patching (by quqi99)

    作者:张华  发表于:2016-02-27 版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) GC ...