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

字典序算法思想:

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. document.documentElement与body下clientHeight,scrollHeight等区别

    本次说明仅在chrom环境下,ie等其他浏览器可能不同 1获取显示屏高度(pc和移动端,屏幕分辨率px) window.screen.height => 这个好理解,不多说. 2获取浏览器可视窗 ...

  2. java源码之HashMap和HashTable的异同

    代码版本 JDK每一版本都在改进.本文讨论的HashMap和HashTable基于JDK 1.7.0_67 1. 时间 HashTable产生于JDK 1.1,而HashMap产生于JDK 1.2.从 ...

  3. Oracle expdp导出多表或表中的部分数据

    http://blog.itpub.net/16582684/viewspace-755072/

  4. Intellij IDEA插件 - Scroll From Source

    Intellij IDEA插件 - Scroll From Source 学习了:http://blog.csdn.net/luonanqin/article/details/41088171 可以自 ...

  5. [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter

    Every Angular CLI generated project comes already with Karmapreinstalled as well a couple of executa ...

  6. Aizu - 2306 Rabbit Party (DFS图论)

    G. Rabbit Party Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO f ...

  7. c/c++常见试题

  8. BZOJ2154: Crash的数字表格 & BZOJ2693: jzptab

    [传送门:BZOJ2154&BZOJ2693] 简要题意: 给出n,m,求$\sum_{i=1}^{n}\sum_{j=1}^{m}LCM(i,j)$ 题解: 莫比乌斯反演(因为BZOJ269 ...

  9. NOIP2017提高组模拟赛 10 (总结)

    NOIP2017提高组模拟赛 10 (总结) 第一题 机密信息 FJ有个很奇怪的习惯,他把他所有的机密信息都存放在一个叫机密盘的磁盘分区里,然而这个机密盘中却没有一个文件,那他是怎么存放信息呢?聪明的 ...

  10. bzoj3442: 学习小组(费用流好题)

    3442: 学习小组 题目:传送门 题解: 超级好题啊大佬们的神题!建图肥肠灵性!感觉自己是星际玩家... 首先呢st直接向每个人连边,容量为min(k,喜欢的小组个数),费用为0 然后每个人再向ed ...