UVALIVE 4556 The Next Permutation
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的更多相关文章
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 逆序数 UVALive 6508 Permutation Graphs
题目传送门 /* 题意:给了两行的数字,相同的数字连线,问中间交点的个数 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 用归并排序或线段树求.想到了就简单了 ...
- UVaLive 11525 Permutation (线段树)
题意:有一个由1到k组成的序列,最小是1 2 … k,最大是 k k-1 … 1,给出n的计算方式,n = s0 * (k - 1)! + s1 * (k - 2)! +… + sk-1 * 0!, ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 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 ...
随机推荐
- erl_0016 《硝烟中的erlang》 读书笔记003 “error_logger 爆炸”
error_logger 爆炸 具有讽刺意味的是,负责错误日志的进程竟然是最为脆弱的之一.在Erlang的缺省安装中,error_logger39负责记录日志(写入磁盘或者发送到网络上),它的速度要比 ...
- bzoj 4447 小凸解密码
bzoj 4447 小凸解密码 先将原始状态的 \(B\) 处理出来,可以发现,若不修改,则每次指定的起始位置不同,对这个环 \(B\) 带来的影响只有 \(B_0\) 不同,即每次 \(B_0=A_ ...
- BZOJ3144 Hnoi2013 切糕 【网络流】*
BZOJ3144 Hnoi2013 切糕 Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的 ...
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
- HDU2896 病毒侵袭 【AC自动机】
HDU2896 病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一 ...
- EXCEL某列长度超过255个字符导入SQL SERVER2005的处理方法
将注册表中 Jet引擎. HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Jet/4.0/Engines/Excel 如果是:ACE引擎. HKEY_LOCAL_MACHI ...
- python环境搭建-Linux系统下python2.7升级python3.5.2步骤
首先Python 查看版本 , 在Linux下特别注意权限问题,创建目录时候切记给予权限 如果是 ubnutu 请使用首先切换到 sudo su , 否则 make install 会出现问题.. 升 ...
- 船长带你看书——《selenium2 python 自动化测试实战》(1)
有很多selenium的教程,你没有从头看,当你看的时候已经很难跟上作者的节奏了.现在,你有机会再从头学一次了.从今天开始,船长会从最基本的知识开始,和大家一起学selenium.能不能跟着一起就看大 ...
- HDU4864 Task
题意 Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, t ...
- windows获取文件夹下所有文件名的方法
方法一:tree命令 TREE——显示磁盘目录结构命令 功能:显示指定驱动器上所有目录路径和这些目录下的所有文件名. 格式:TREE [盘符:][\目录] [/F] [/A] 使用说明:使用/F参数时 ...