题意与解析

这题其实特别简单,求给定排列的后继。使用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. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  2. 【翻译】苹果官网的命名规范之 Code Naming Basics-General Principles

    苹果官方原文链接:General Principles 代码命名基本原则:通用规范   代码含义清晰 尽可能将代码写的简洁并且明白是最好的,不过代码清晰度不应该因为过度的简洁而受到影响.例如: 代码 ...

  3. 推荐几个Mac/Linux下比较好用的工具

    1.Tmux,连接开发机可以让在任务在开发机一直执行,不用nohup &这种了也相对稳定,还有session可以记录当时的状态. 常用命令: tmux new -s name 指定名字开启一个 ...

  4. he lover you 用python 搞为 eh revol uoy 。

    1.字符串序列翻转  ----   字符串切片 a_str = “abc”[::-1] 2.字符串分割: b_str  = ‘ ds fdf  ds ’ b_str .split(" 空格& ...

  5. Aspose.cell C# 操作excel(通过批注信息给单元格赋值、批注信息获取公式得出结果并转PNG)

    if (fileName == "") return ""; var CurrentRow = 0; Workbook work = new Workbook( ...

  6. 请对比 Exception 和 Error,另外,运行时异常与一般异常有什么区别?

    error指的是不可预料的错误,可能会导致程序宕机:而exception指的是在程序运行中可以预见的异常,而异常分为检查异常与一般异常,检查异常需要在程序中显示捕获并处理,一般异常可以通过程序编码来进 ...

  7. 小B的询问(莫队)

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...

  8. JS对象和数组在谷歌浏览器中引用存储的表现

    大家都知道JS的数据分为基本类型和引用类型.具体什么不说了,今天主要说说对象和数组作为引用类型在谷歌浏览器中的表现. 首先,问题是这么发现的.我在控制台使用console打印了一个数组,然后对数组进行 ...

  9. js 判断两个时间相差的天数

    judgeDay(sDate1, sDate2) { const sDate1 = `${new Date(sDate1).getFullYear()}-${new Date(sDate1).getM ...

  10. Hadoop(14)-MapReduce框架原理-切片机制

    1.FileInputFormat切片机制 切片机制 比如一个文件夹下有5个小文件,切片时会切5个片,而不是一个片 案例分析 2.FileInputFormat切片大小的参数配置 源码中计算切片大小的 ...