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. LOJ#539. 「LibreOJ NOIP Round #1」旅游路线

    n<=100,m<=1000的图,在此图上用油箱容量C<=1e5的车来旅行,旅行时,走一条边会耗一单伟油,在点i时,若油量<ci,则可以把油以pi的价格补到ci,pi<= ...

  2. Tomcat服务器调优

    一,目标:优化tomcat来提高访问的并发能力. 服务器提供的内存,cpu,以及硬盘的性能对数据的处理起决定性作用. tomcat的3种运行模式 tomcat的运行模式有3种: 1. bio默认的模式 ...

  3. SystemTap 学习笔记 - 安装篇

    https://segmentfault.com/a/1190000000671438 在安装前,需要知道下自己的系统环境,我的环境如下: uname -r 2.6.18-308.el5 Linux ...

  4. uva 1411 Ants (权值和最小的完美匹配---KM算法)

    uva 1411 Ants Description Young naturalist Bill studies ants in school. His ants feed on plant-louse ...

  5. Windows Server 2012关机的几种方法

    综合使用方法如下: 1.shutdown -r –f     強制重新启动 2.shutdown –s –f    強制关闭 方法/步骤 1 首先把鼠标移到任务栏右下角,时间往右一点小空位,稍微停留一 ...

  6. AutoCAD如何添加网络打印机 添加公司打印机

    1 请确认你已经连接了这台网络打印机,你没法打AutoCAD图纸,但是应该能打Word文档.能打Word文档说明你驱动装好了,通信也没问题,只是AutoCAD的配置不对导致的.   2 点击打印,打印 ...

  7. spring mvc +Mybatis3.1 整合的时候异常

    在使用Mybatis3.10+spring3.10+mybatis-spring-1.0.0集成,使用spring 时发生例如以下错误: 严重: Servlet.service() for servl ...

  8. Azure Pack演示样例缩放部署架构

  9. 仅仅需一步教你解决Win10下Android Studio terminal无法使用的问题

    Android Studio集成和加入了一些有用的工具.当中一个便是terminal. 在Windows平台下Android Studio中的terminal在原理上实际使用的是window中的cmd ...

  10. Android小技巧总结1

    0.Android彻底退出引用程序的方法 1) Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid()); / ...