题意与解析

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

【个人训练】(UVa146)ID Codes的更多相关文章

  1. uva146 ID codes

    Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  2. UVa-146 - ID Codes(下一个排列)

    /* ID Codes It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  3. UVA-146 ID Codes

    It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...

  4. Brute Force & STL --- UVA 146 ID Codes

     ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...

  5. POJ 1146:ID Codes

    ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...

  6. UVA 146 ID Codes(下一个排列)

    C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  7. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

  8. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  9. POJ 1146 ID Codes (UVA146)

    // 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...

随机推荐

  1. WPF学习笔记(7):DataGrid中数字自定义格式显示

    DataGrid中数据显示如下图,数据格式比较杂乱.希望达到以下要求:(1)所有数据保留两位小数:(2)超过1000的数字显示千分位:(3)如果数据为0,不显示. 首先想到用StringFormat进 ...

  2. PHP中级面试经历

    资源来自网络:http://www.2cto.com/kf/201304/204749.html 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) 2.echo(),p ...

  3. Eclipse插件的卸载和安装

    Eclipse 卸载插件: 右下角会有卸载进度 卸载完后 然后需要重启 Eclipse安装插件 选择本地下载好的插件 点击 Ok 插件下载地址:https://jaist.dl.sourceforge ...

  4. 使用transfor让图片旋转

    材料:Transform,onmouseout,onmouseover css: html: js:

  5. 自己做的HTML

    <html> <body background="http://img1.imgtn.bdimg.com/it/u=821335874,2927998559&fm= ...

  6. Vue nodejs商城项目-商品列表页面组件

    data(){        return {            goodsList:[], // 商品列表            priceFilter:[ // 价格区间数组          ...

  7. 安装VMware,出现没有虚拟网络适配器的问题

    遇到错误:安装VMware Workstation Pro这个软件,网络适配器中没有虚拟网卡,导致无法上网 解决方法:遇到这个问题,我就第一时间就去网上搜索解决方法,方案有很多,但是试了很多个还是不行 ...

  8. 动态规划(一)POJ1163

    动态规划算法是比较实用的算法之一,结合实际问题能更好的熟悉这个算法 下面以POJ1163为例子 1. 首先是题目大意 :在给定的一个数字三角形中找到一条自上而下的路径,路径每一步只能左下或者右下,最后 ...

  9. 转:深入理解jvm

    深入理解JVM 原文链接:http://www.cubrid.org/blog/dev-platform/understanding-jvm-internals 每个使用Java的开发者都知道Java ...

  10. XPath Helper的安装使用

    XPath Helper的安装使用 xpath helper 是一款chrome浏览器插件,主要用来分析当前网页信息的xpath,在抓取数据时一般会使用到xpath. 安装 下载地址:http://c ...