uva146-枚举,排列
题意:
输入最多150个小写字母,在字典序增大的方向,求下一个排列是什么.
模拟枚举,最后一个字符是递归的最后一层(n层),那么把它弹出栈(还剩n-1层),如果n-1层的字符比第n层小,说明把n层的字符移到n-1层是一个更大的排列,
同样,对于任意第k层,比较k+1,k+2.......n层
ac代码,一个用了库,一个自己写的
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include<time.h>
#include <stdlib.h>
#include <algorithm>
using namespace std; struct Stack
{
int s[200];
int si = -1;
void push(int c)
{
s[++si] = c;
}
int pop()
{
if(si == -1)
return -1;
int c = s[si--];
return c;
}
}; int main()
{
//freopen("d:\\1.txt", "r", stdin);
string no = "No Successor";
while (cin)
{
string str;
cin >> str;
if(str == "#")
return 0;
if(next_permutation(str.begin(), str.end()))
cout << str << endl;
else
cout << no << endl;
}
return 0;
}
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include<time.h>
#include <stdlib.h>
using namespace std; struct Stack
{
int s[200];
int si=-1;
void push(int c)
{
s[++si] = c;
}
int pop()
{
if(si == -1)
return -1;
int c = s[si--];
return c;
}
}; int main()
{
//freopen("d:\\1.txt", "r", stdin);
string no = "No Successor";
while (cin)
{
string str;
cin >> str;
if(str == "#")
return 0;
int length = str.length();
Stack s;
//26 low letter
int num[26];
memset(num, 0, sizeof(num));
for(int i = 0; i < length; i++)
s.push(str.at(i));
int l = 26;
int j = -1;
while (true)
{
int k = s.pop();
if(k == -1)
{
break;
}
num[k - 'a']++;
for(int i = k - 'a' + 1; i < l; i++)
if(num[i] != 0)
{
j = i;
break;
}
if(j != -1)
break;
}
if(j == -1)
{
cout << no << endl;
}
else
{
s.push(j+'a');
num[j]--;
for(int i = 0; i < l; i++)
{
while (num[i]--)
{
s.push(i + 'a');
}
}
string ss = "";
while (true)
{
j = s.pop();
if(j==-1)
break;
ss += (char)j;
}
for(int i = ss.length()-1;i>=0;i--)
cout<<ss.at(i);
cout<<endl; }
}
return 0;
}
uva146-枚举,排列的更多相关文章
- 蓝桥杯 2014本科C++ B组 六角填数 枚举排列
		标题:六角填数 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填 ... 
- poj 2585 Window Pains 暴力枚举排列
		题意: 在4*4的格子中有9个窗体,窗体会覆盖它之下的窗体,问是否存在一个窗体放置的顺序使得最后的结果与输入同样. 分析: 在数据规模较小且不须要剪枝的情况下能够暴力(思路清晰代码简单),暴力一般分为 ... 
- POJ 3187 杨辉三角+枚举排列 好题
		如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开 ... 
- UVa 140 (枚举排列) Bandwidth
		题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ... 
- UVa 216 Getting in Line【枚举排列】
		题意:给出n个点的坐标,(2<=n<=8),现在要使得这n个点连通,问最小的距离的和 因为n很小,所以可以直接枚举这n个数的排列,算每一个排列的距离的和, 保留下距离和最小的那个排列就可以 ... 
- UVa 729 The Hamming Distance Problem【枚举排列】
		题意:给出数组的长度n,给出h,表示这个数组里面含有h个1,求其所有的排列 用next_permutation就可以了 #include<iostream> #include<cst ... 
- UVa 140 Bandwidth【枚举排列】
		题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ... 
- UVA - 10098 - Generating Fast   (枚举排列)
		思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ... 
- poj 2038 Team Rankings 枚举排列
		//poj 2038 //sep9 #include <iostream> #include <algorithm> using namespace std; char s[1 ... 
- 枚举所有排列-STL中的next_permutation
		枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ... 
随机推荐
- [LeetCode&Python] Problem 868. Binary Gap
			Given a positive integer N, find and return the longest distance between two consecutive 1's in the ... 
- null 与  undefinded
			null表示"没有对象",即该处不应该有值.典型用法是: (1) 作为函数的参数,表示该函数的参数不是对象. (2) 作为对象原型链的终点. Object.getPrototype ... 
- Nginx访问限制模块limit_conn_zone 和limit_req_zone配置使用
			nginx可以通过limit_conn_zone 和limit_req_zone两个组件来对客户端访问目录和文件的访问频率和次数进行限制,另外还可以善用进行服务安全加固,两个模块都能够对客户端访问进行 ... 
- 【vue】vue-cli 脚手架项目简介(一) - package.json
			vue-cli是用来生成 vue项目的命令行工具,它的使用方法是这样的: vue init <template-name> <project-name>第二个参数 templa ... 
- 【maven】Maven根据Profile读取不同配置环境配置文件
			开发需求:在日常开发中,我们大多都会有开发环境(dev).测试环境(test).生产环境(product),不同环境的参数肯定不一样,我们需要在打包的时候,不同环境打不同当包,如果手动改,一方面效率低 ... 
- LeetCode  Factorial Trailing Zeroes Python
			Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ... 
- nexage  video  asset tag
			video ad can't show InLine must match the example ,and xml content is Case Sensitive https:/ ... 
- oracle C# 访问
			使用oracle的odp.net 进行oracle数据库的访问对于进行oracle数据库的开发来说是比较方便的,使用的方式与ADO.net 是一致的. 一下为使用的测试 1. 安装必要的oracle ... 
- CodeIgniter  安装指导
			CodeIgniter 安装分为四个步骤: 解压缩安装包. 把 CodeIgniter 文件夹和里面的文件上传到你的服务器.通常 index.php 在根目录. 用任何文本编辑器打开 applicat ... 
- spring 使用 maven profile
			先看看 maven 定义 profile 的写法 <!-- profiles --> <profiles> <profile> <activation> ... 
