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 //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...
随机推荐
- ajax请求成功回调函数没有执行问题
如下常见的ajax前端请求,请求成功后success:function(result){ 这里的数据没有执行 }: $.ajax({ type:"post", url:" ...
- fullPage的使用
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 51Nod 1072:威佐夫游戏 (威佐夫博奕)
1072 威佐夫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数 ...
- 《DSP using MATLAB》Problem 4.24
Y(z)部分分式展开, 零状态响应部分分式展开, 零输入状态部分分式展开,
- 第2季:从官方例程深度学习海思SDK及API
2.1.官方mppsample的总体分析2.1.sample的整体架构(1)sample其实是很多个例程,所以有很多个main(2)每一个例程面向一个典型应用,common是通用性主体函数,我们只分析 ...
- ThinkPHP 的一个神秘版本 ThinkPHP 1.2
ThinkPHP 的一个神秘版本 ThinkPHP 1.2 询问过 ThinkPHP 官网的小伙伴都知道,偶尔 ThinkPHP 故障时会出现 ThinkPHP 1.2(下次看到就截图下来). 但是我 ...
- SOALog
项目地址 : https://github.com/kelin-xycs/SOALog SOALog 为 SOA 架构 提供一种 松耦合 乐观 的 数据一致性 解决方案,说白了这个组件的功能就是 记 ...
- 可靠的推送IM消息
一. 报文类型: 1.请求报文(request,后简称为为R): 2.应答报文(acknowledge,后简称为A): 3.通知报文(notify,后简称为N). R:客户端主动发送给服务器 ...
- php比较全的友好时间显示
分享一个php友好的比较完成的时间格式化函数,包括‘刚刚’,'几秒之前',‘几分钟前’,'几小时前',几天前,几周前,几个月前等.调用方式很简单,是从ThinkSNS 里面拿出来的. /** * 友好 ...
- MapReduce-朴素贝叶斯
朴素贝叶斯(Native Bayes),贝叶斯在现实使用中是怎么玩的?不是根据A|B的概率获得B|A的概率,在现实中的玩法是: 首先要明白贝叶斯是一种分类算法,因为是概率所以,他的应用领域其实是比较, ...