ACM POJ 1146 ID Codes
题目大意:输入一个字符串。输出它的下一个字典序排列。
字典序算法思想:
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的更多相关文章
- poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6229 Accepted: 3737 Descript ...
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- POJ 1146 ID Codes (UVA146)
// 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...
- (组合数学3.1.1.1)POJ 1146 ID Codes(字典序法)
/* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...
- 1146 ID Codes
题目链接: http://poj.org/problem?id=1146 题意: 给定一个字符串(长度不超过50), 求这个字符串的下一个字典序的字符串, 如果已经是最大字典序, 那么输出 " ...
- 【水一发next_permutation】poj 1146——ID Codesm
来源:点击打开链接 求字典序下一位,没有直接输出没有.全排列函数秒水过. #include <iostream> #include <algorithm> #include & ...
- POJ 1146:ID Codes
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...
- uva146 ID codes
Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...
- Brute Force & STL --- UVA 146 ID Codes
ID Codes Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...
随机推荐
- STM32 软件模拟 IIC 代码,标准库、HAL库可用
#ifndef _IIC_H #define _IIC_H #include "stdio.h" #include "stm32f1xx_hal.h" /* 定 ...
- Oracle 后台进程介绍
一 进程分类: 1.服务器进程(server process): 依据客户请求完毕工作.如接收和处理应用发送的SQL语句 2.后台进程(background process): 随数据库而启动,用于完 ...
- bzoj4823: [Cqoi2017]老C的方块(最小割)
4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强 黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...
- What is the difference between Web Farm and Web Garden?
https://www.codeproject.com/Articles/114910/What-is-the-difference-between-Web-Farm-and-Web-Ga Clien ...
- CNN tflearn处理mnist图像识别代码解说——conv_2d参数解释,整个网络的训练,主要就是为了学那个卷积核啊。
官方参数解释: Convolution 2D tflearn.layers.conv.conv_2d (incoming, nb_filter, filter_size, strides=1, pad ...
- MVC开发模式详解
转自:https://blog.csdn.net/qq_33991989/article/details/78966071 MVC设计模式详解 1.Model-View-Controller(模型-视 ...
- [jzoj 4879] [NOIP2016提高A组集训第11场11.9] 少女觉 解题报告 (贪心)
题目链接: http://172.16.0.132/senior/#main/show/4879 题目: 在幽暗的地灵殿中,居住着一位少女,名为古明地觉.据说,从来没有人敢踏入过那座地灵殿,因为人们恐 ...
- Nginx访问VM虚拟机CentOS 7系统与本地Windows系统共享目录403
用VMware安装了CentOS7系统,并搭建了Nginx,MySQL,PHP的web项目运行环境,为了方便Windows本地主机进行程序调试把Windows本地项目目录共享到了虚拟机CentOS中的 ...
- JavaScript学习记录一
title: JavaScript学习记录一 toc: true date: 2018-09-11 18:26:52 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...
- luogu 2679 子串
子串 史上最简短的一篇博客,毕竟看题解ac心疼我的kmp /* f[i][j][k][0/1]表示A的前i个,B的前j个,用到了k个子串,当前字符选或者不选. 所以f[0][0][0][0]的方案数为 ...