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 //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...
随机推荐
- hihocoder1545 : 小Hi和小Ho的对弈游戏(树上博弈&nim博弈)
描述 小Hi和小Ho经常一起结对编程,他们通过各种对弈游戏决定谁担任Driver谁担任Observer. 今天他们的对弈是在一棵有根树 T 上进行的.小Hi和小Ho轮流进行删除操作,其中小Hi先手. ...
- CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive in ...
- 《DSP using MATLAB》Problem 3.8
2018年元旦,他乡加班中,外面尽是些放炮的,别人的繁华与我无关. 代码: %% ----------------------------------------------------------- ...
- ES6必知必会 (三)—— 数组和对象的拓展
数组的扩展 1.拓展运算符('...'),它相当于rest参数的逆运算,用于将一个数组转换为用逗号分隔的参数序列: console.log(...[1, 2, 3]) // 1 2 3 console ...
- nginx unit nodejs 模块试用
unit 对于nodejs 的支持是在10.25 发布的,基本能用,但是依然有好多问题,当前在测试的时候就发现,请求之后会block , 相关的issue 已经有人反馈了,最好使用源码编译,方便测 ...
- 按的第一个greasemonkey插件:评论时可以粘贴啦~~
原来的样子:如果按ctrl+V会跳出错误
- Java HashMap原理
HashMap存储结构 HashMap中数据的存储是由数组与链表一起实现的 数组寻址非常容易,其时间复杂度为O(1),但是当要插入或删除数据时,时间复杂度就会变为O(n).链表插入和删除操作的内存复杂 ...
- JMeter--使用URL回写来处理用户会话
如果测试的Web应用系统使用URL回写而非Cookie来保存会话信息,那么测试人员需要做一些额外的工作来测试web站点 为了正确回应URL回写,JMeter需要解析从服务器收到的HTML,并得到唯一的 ...
- PHP解析xml文件时报错:I/O warning : failed to load external entity
在代码顶部增加 libxml_disable_entity_loader(false); libxml_disable_entity_loader()作用是设置是否禁止从外部加载XML实体,设为tru ...
- 织梦ask标签的调用
EDE 问答首页调用标签 标签名称: ask 功能说明:问答调用标签 适用范围:全局使用 基本语法: {dede:ask row='6' qtype='new' tid='0' titlelen='2 ...