【个人训练】(UVa146)ID Codes
2024-09-28 00:26:42
题意与解析
这题其实特别简单,求给定排列的后继。使用stl(next_permutation)可以方便地解决这个问题。但是,想要自己动手解就是另外一回事了。我的解法是从后往前找到第一个$a_i$比$a_j$小($i<j$)的,然后交换之,接下来i->end范围内重排序即可。
代码
/* ***********************************************
Author :Sam X
Created Time :2018年01月16日 星期二 08时44分26秒
File Name :uva146.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
string str;
while(cin>>str)
{
bool ok=false;
if(str[0]=='#') break;
int len=str.length();
for(int i=len-1;i>=1;--i)
{
for(int j=i-1;j>=0;--j)
{
if(str[j]<str[i])
{
ok=true;
swap(str[i],str[j]);
auto iter=str.begin();
advance(iter,j+1);
sort(iter,str.end());
break;
}
}
if(ok) break;
}
if(ok) cout<<str<<endl;
else cout<<"No Successor"<<endl;
}
return 0;
}
最新文章
- SQL Server编程(02)自定义函数
- jQuery 获取checkbox 获取值
- 为python-sproto添加map支持
- 关闭Windows Update更新驱动程序
- C++类构造析构调用顺序训练(复习专用)
- zoj 3690 Choosing number
- SQL语句执行顺寻
- POJ-2926 Requirements 最远曼哈顿距离
- Java获取时间与系统时间相差8小时终极解决方案
- 基于内容的自适应变长编码[CAVLC]
- Invalid file permission Please regenerate them with cacaoadm create-keys --force
- 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
- 积累的VC编程小技巧之对话框
- 邻里街坊 golang入坑系列
- C++调用C方法
- Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台
- [JOISC2014]たのしい家庭菜園
- cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式
- WebForm - cookie赋值乱码问题
- iphone6/6+ 适配心得