题目大意:输入一个字符串。输出它的下一个字典序排列。

字典序算法思想:

1.从右向左寻找字符串找出第一个a[i]<a[i+1]的位置i;

2.从右向左找出第一个大于a[i]的元素a[j];

3.swap(a[i],a[j])

4.将a[i+1]......到a[stelen(a)]倒序

5.输出a

代码例如以下:

#include<iostream>
#include<cstdio>
#include <cstring>
#include<algorithm>
#include<cstdlib>
using namespace std; inline void swap(int &a,int &b){
int temp;
temp=a;
a=b;
b=temp;
} void Print(int a[],int n){
for(int i=1;i<=n;i++){
printf("%c",a[i]+'0');
if(i==n) cout<<"\n";
}
return ;
} int main(){ int Dire[100],q;
char a[100];
int r,l,n,t;
int count=0,c=1;
while(scanf("%s",a)&&strcmp(a,"#")!=0){
n=strlen(a);//求出字符串的长度
for(int i=1;i<=n;i++){//初始化待排列的数字串
Dire[i]=a[i-1]-'0';
}
int i=n-1;
l=0;
while(i>0){
if(Dire[i]<Dire[i+1]){//找到Dire[i]<Dire[i+1]
l=i;
break;
}
else{
i--;
}
}
if(l){ //存在兴许排列
for(int j=n;j>l;j--){
if(Dire[j]>Dire[l]){ //从右向左找到第一个Dire[j]>Dire[i]
r=j;
break; //找到第一个Dire[j]时。即跳出循环
}
}
swap(Dire[l],Dire[r]); //交换Dire[j]>Dire[i]
for(int p=l+1,q=n;p<q;p++,q--)//倒序
swap(Dire[p],Dire[q]);
Print(Dire,n);
}
else{
cout<<"No Successor"<<endl;
}
}
return 0;
}

ACM POJ 1146 ID Codes的更多相关文章

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

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

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

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

  3. POJ 1146 ID Codes (UVA146)

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

  4. (组合数学3.1.1.1)POJ 1146 ID Codes(字典序法)

    /* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...

  5. 1146 ID Codes

    题目链接: http://poj.org/problem?id=1146 题意: 给定一个字符串(长度不超过50), 求这个字符串的下一个字典序的字符串, 如果已经是最大字典序, 那么输出 " ...

  6. 【水一发next_permutation】poj 1146——ID Codesm

    来源:点击打开链接 求字典序下一位,没有直接输出没有.全排列函数秒水过. #include <iostream> #include <algorithm> #include & ...

  7. POJ 1146:ID Codes

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

  8. uva146 ID codes

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

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

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

随机推荐

  1. js特效——自动滚动

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 【codeforces 733F】Drivers Dissatisfaction

    [题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...

  3. 2015 Multi-University Training Contest 3 hdu 5318 The Goddess Of The Moon

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. SQL SERVER-identity | @@identity | scope_identity

    主键自增 IDENTITY(1,1),MS SQL Server 使用 IDENTITY 关键字来执行 auto-increment 任务. 在上面的实例中,IDENTITY 的开始值是 1,每条新记 ...

  5. ASP.NET-表单验证-DataAnnotations

    DataAnnotations  [数据注解,数据注释] 需要引入两个脚本文件 <script src="@Url.Content("~/Scripts/jquery.val ...

  6. shell文本过滤编程(一):grep和正則表達式

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] Linux系统中有非常多文件,比方配置文件.日志文件.用户文件等 ...

  7. ioctl.h 分析

    ioctl.h 分析 我自己画了个解析图...不要嫌弃丑啊.. . 哈哈 type The magic number. Just choose one number (after consulting ...

  8. thinkphp项目上传到github,为什么缺少很多文件

    thinkphp项目上传到github,为什么缺少很多文件 问题: 把tp5项目push到码云(类似github)上,为什么没有thinkphp这个核心库? 然后我看了下码云和github上,官方的t ...

  9. ThinkPHP5.0框架开发--第6章 TP5.0 请求和响应

    ThinkPHP5.0框架开发--第6章 TP5.0 请求和响应 第6章 TP5.0 请求和响应 ===================================== 上次复习 1.新建控制器 ...

  10. ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用!因此,ES transport client可以同步调用也可以异步(不过底层的socket必然是异步实现)

    ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用! 因此,ES tra ...