4556 The Next Permutation
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’.
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.
Sample Input
3
1 123
2 279134399742
3 987
Sample Output
1 132
2 279134423799
3 BIGGEST

題意:操作字符串,通過調整字符串的順序得到一個新的字符串,新的字符串的数值是比原字符串的数大的最小值;

题解:由于是最小值,我们可以从后往前遍历字符串,当str[i]>str[i-1]时,pos=i-1,此位置就是我们要调整字符串的起始位置(说明我们可以调整顺序使原来的字符串的值增大),然后从我们要截取的字串中找到第一个比上pos大的值交换,然后将字串排序即可,若没有条件满足str[i]>str[i-1],则通过调整字符顺序没有比原串更大的数值。

#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
int m,n,k,p;
int ans[];
int dis[][];
int di[][]= {{-,},{,},{,-},{,}};
map<ll,ll>::iterator it;
int main()
{
cin>>m;
while(m--)
{
string str;
cin>>n>>str;
int pos=-;
for(int i=str.size()-; i>=; i--)
{
if(str[i]>str[i-])
{
pos=i-;
break;
}
}
if(pos==-)
{
cout<<n<<" "<<"BIGGEST"<<endl;
continue;
}
string s,arr;
s=str.substr(pos);
int cnt=s[];
for(int i=''; i<=''; i++)
{ if(s.find(i)!=string::npos&&i>cnt)
{
swap(s[],s[s.find(i)]);
break;
}
}
sort(s.begin()+,s.end());
cout<<n<<" "<<str.substr(,pos)<<s<<endl;;
} }

UVALIVE 4556 The Next Permutation的更多相关文章

  1. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  2. 逆序数 UVALive 6508 Permutation Graphs

    题目传送门 /* 题意:给了两行的数字,相同的数字连线,问中间交点的个数 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 用归并排序或线段树求.想到了就简单了 ...

  3. UVaLive 11525 Permutation (线段树)

    题意:有一个由1到k组成的序列,最小是1 2 … k,最大是 k k-1 … 1,给出n的计算方式,n = s0 * (k - 1)! + s1 * (k - 2)! +… + sk-1 * 0!, ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  6. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  7. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

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

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

  9. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. BZOJ2431 HAOI2009 逆序对数列 【DP】*

    BZOJ2431 HAOI2009 逆序对数列 Description 对于一个数列ai{a_i}ai​,如果有i<j且ai>aja_i>a_jai​>aj​,那么我们称aia ...

  2. hadoop入门手册5:Hadoop【2.7.1】初级入门之命令:文件系统shell2

    问题导读 1.改变hdfs文件的权限,需要修改哪个配置文件?2.获取一个文件的或则目录的权限,哪个命令可以实现?3.哪个命令可以实现设置访问控制列表(ACL)的文件和目录? 接上篇:Hadoop[2. ...

  3. Linux下gdb线程的调试

    多线程的调试命令 1.info threads: 这条命令显示的是当前可调试的所有线程,GDB会给每一个线程都分配一个ID.前面有*的线程是当前正在调试的线程. 2.thread ID: 切换到当前调 ...

  4. git clone遇到的[ssh: connect to host github.com port 22]

    起因 在学习递归的时候,对汉诺塔小研究了一番,参考网上写了个demo,后面就想同步到github. 过程 这台电脑是新电脑,所以需要先本地生成ssh key:ssh-keygen -t rsa -C ...

  5. Iterations --codility

    lesson 1:Iterations 1. BinaryGap-----[100%] Find longest sequence of zeros in binary representation ...

  6. 可以随时查找的max栈和max队列——面试

    这是面试中比较常见的题目,max队列也是编程之美里的一道题 对于max的栈,有个比较简单的办法就是,每次入栈前判断栈顶元素与正在入栈的元素哪个大,哪个大就哪个入栈 对于队列,我们知道可以用两个栈来实现 ...

  7. C# 6.0 新特性收集

    先来看一段Json.Net的代码 public JObject ToJson() { var result = new JObject(); result["X"] = X; re ...

  8. mysql的三种安装方式(详细)

    安装MySQL的方式常见的有三种: rpm包形式 通用二进制形式 源码编译 1,rpm包形式 (1) 操作系统发行商提供的 (2) MySQL官方提供的(版本更新,修复了更多常见BUG)www.mys ...

  9. 【转】Jmeter测试报表相关参数说明

    Jmeter测试报表相关参数说明 采用Jmeter测试工具对web系统作的负载测试,得出的响应报表,数据比较难懂,现作一具体说明. 以下是在一次具体负载测试中得出的具体数值,测试线程设置情况为:线程数 ...

  10. 1006 Sign In and Sign Out (25)(25 分)思路:普通的时间比较题。。。

    1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...