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 ...
随机推荐
- HDU - 5306 Gorgeous Sequence (吉司机线段树)
题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...
- SSH使用密钥登录并禁止密码登录
#1 新建用于登录的用户useradd -p `echo "KYmO4ClPt1" | openssl passwd -1 -salt $(< /dev/urandom tr ...
- 20165212 预备作业3 Linux安装及学习
20165212 预备作业3Linux安装及学习 Linux虚拟机的安装过程 我像大部分同学一样,通过助教学姐给的Ubuntu下载地址下载映像文件.VB,但是屡次出现问题,不停的闪出一下错误提示窗口: ...
- Flask第四篇——第一个程序
看,是第一个程序,不是脚本哦~~ 把生成的模板上面加上编码,然后运行,看输出台: 上图中有 Running on http://127.0.0.1:5000/ 点击那个地址,就可以在浏览器看到代码运行 ...
- 浅谈ES6新特性
ES6的了解 新增模板字符串(为JavaScript提供了简单的字符串插值功能).箭头函数(操作符左边为输入的参数,而右边则是进行的操作以及返回的值Inputs=>outputs.).for-o ...
- C#中的依赖注入那些事儿
目录 目录 1 IGame游戏公司的故事 1.1 讨论会 1.2 实习生小李的实现方法 1.3 架构师的建议 1.4 小李的小结 2 探究依赖注入 2.1 故事的启迪 2.2 正式定义依赖注入 3 依 ...
- Linux环境下安装Websphere8.5.5
首先安装包资源: https://pan.baidu.com/s/1Jvkqe3WMgNQ3bn3ggYGhAQ 下面是Installation Manager安装包 agent.installer. ...
- 利用 netsh 给 mysql 开启多端口监听
利用 netsh 给 mysql 开启多端口监听 标题党,实际并不是真的多端口监听,只是端口转发而已. 由于某种特殊原因需要 mysql 服务器多个端口监听. mysql 服务器本身是不支持的,但可以 ...
- 理想中的 PCB 文件格式
理想中的 PCB 文件格式 因为平时写代码使用 git,在画 PCB 也使用 git 来管理 PCB 文件. 但是 PCB 文件是二进制的,所以在比较时非常麻烦. 虽然 PCB 文件可以导出 文本文件 ...
- [转]java中byte转换int时为何与0xff进行与运算
在剖析该问题前请看如下代码public static String bytes2HexString(byte[] b) { String ret = ""; for (int ...