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. erl_0014 《硝烟中的erlang》 读书笔记001 “绪论”

    1.大家听说Erlang,往往是因为其对高并发的良好支持.其实,Erlang的核心特征是容错,从某种程度上讲,并发只是容错这个约束下的一个副产品.容错是Erlang语言的DNA,也是和其他所有编程语言 ...

  2. 报错【Expression user.achievement is undefined on 】

  3. BZOJ4903 UOJ300 CTSC2017 吉夫特 【Lucas定理】

    BZOJ4903 UOJ300 CTSC2017 吉夫特 弱弱地放上题目链接 Lucas定理可以推一推,发现C(n,m)是奇数的条件是n" role="presentation&q ...

  4. 在后台new出页面(组件)

    Page p = new Page();            Control u = p.LoadControl("~/folderName/controlName.ascx") ...

  5. .NET平台上的Model-View-Presenter模式实践

    为什么要写这篇文章 笔者当前正在负责研究所中一个项目,这个项目基于.NET平台,初步拟采用C/S部署体系,所以选择了Windows Forms作为其UI.经过几此迭代,我们发现了一个问题:虽然业务逻辑 ...

  6. 自定义显示提示一段时间自动消失ShowMsgText控件

    public partial class ShowMsgText : Label { public string TextMsg { get { return Text; } set { timer1 ...

  7. 搭建一个免费的,无限流量的Blog----github Pages和Jekyll入门(转)

    转自:http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html 喜欢写Blog的人,会经历三个阶段. 第一阶段,刚接触Blog, ...

  8. printf()_scanf()_取余运算符与取模运算符

    基本的输入和输出函数的用法 printf();四种用法 1.printf("字符串\n"); 2.printf("输出控制符",输出参数); 3.printf( ...

  9. C# 7.0 新特性收集

    1.out-variables(Out变量) 2.Tuples(元组) 3.Pattern Matching(匹配模式) 4.ref locals and returns (局部变量和引用返回) 5. ...

  10. CSS 属性单词

    .container {padding:0px; height:90%; width:100%; margin:0;}#header {height:0px; width:100%; padding: ...