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. 【IntelliJ 】设置 IntelliJ IDEA 主题和字体的方法

    2 主题修改 2.1 界面主题修改 如上图所示,依次点击Files -> Settings,进入如下界面: 标注1:主题选择区: 标注2:Darcula.IntelliJ 和 Windows,三 ...

  2. Codeforces Round #489 (Div. 2) B、C

    B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...

  3. 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle

    http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020 求出n个数的和sum,然后用s ...

  4. Java度线程——生产消费问题

    /*JDK1.4版本:生产者,消费者.多生产者,多消费者的问题.if判断标记,只有一次,会导致不该运行的线程运行了.出现了数据错误的情况.while判断标记,解决了线程获取执行权后,是否要运行! no ...

  5. JavaScript错误处理和堆栈追踪

    转自:https://github.com/dwqs/blog/issues/49 有时我们会忽略错误处理和堆栈追踪的一些细节, 但是这些细节对于写与测试或错误处理相关的库来说是非常有用的. 例如这周 ...

  6. 踩坑录-IDEA编辑器:找不到TomcatService或ApplicationServers----TomcatService使用指南

    一.找不到TomcatService或ApplicationServers Setp1. 检查IDEA版本 检查IDEA版本是否为Ultimate(终极版需要激活),Community(社区版免费无需 ...

  7. Python3标准库(二) re模块

    正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...

  8. Lua学习笔记7:时间和日期

    lua中的时间类似于C语言中的时间,例如以下: local time = os.time() print(time) local t = os.date("*t") for k,v ...

  9. Android 最火框架XUtils之注解机制具体解释

    在上一篇文章Android 最火的高速开发框架XUtils中简介了xUtils的基本用法,这篇文章说一下xUtils里面的注解原理. 先来看一下xUtils里面demo的代码: @ViewInject ...

  10. Codeforces(429D - Tricky Function)近期点对问题

    D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...