题意与解析

这题其实特别简单,求给定排列的后继。使用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;
}

最新文章

  1. SQL Server编程(02)自定义函数
  2. jQuery 获取checkbox 获取值
  3. 为python-sproto添加map支持
  4. 关闭Windows Update更新驱动程序
  5. C++类构造析构调用顺序训练(复习专用)
  6. zoj 3690 Choosing number
  7. SQL语句执行顺寻
  8. POJ-2926 Requirements 最远曼哈顿距离
  9. Java获取时间与系统时间相差8小时终极解决方案
  10. 基于内容的自适应变长编码[CAVLC]
  11. Invalid file permission Please regenerate them with cacaoadm create-keys --force
  12. 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
  13. 积累的VC编程小技巧之对话框
  14. 邻里街坊 golang入坑系列
  15. C++调用C方法
  16. Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台
  17. [JOISC2014]たのしい家庭菜園
  18. cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式
  19. WebForm - cookie赋值乱码问题
  20. iphone6/6+ 适配心得

热门文章

  1. 【模板】各种背包问题&amp;讲解
  2. Android学习笔记_65_登录功能本身没有任何特别
  3. MYSQL添加外键关联
  4. Question 20171117 Java中的编码问题?
  5. jQuery Ajax请求后台数据并在前台接收
  6. maven项目打包后war文件丢失配置文件
  7. 关于api接口
  8. Delphi 过程类型
  9. 2.从print到自省
  10. Leecode刷题之旅-C语言/python-58.最后一个单词的长度