题意:

输入最多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-枚举,排列的更多相关文章

  1. 蓝桥杯 2014本科C++ B组 六角填数 枚举排列

    标题:六角填数 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填 ...

  2. poj 2585 Window Pains 暴力枚举排列

    题意: 在4*4的格子中有9个窗体,窗体会覆盖它之下的窗体,问是否存在一个窗体放置的顺序使得最后的结果与输入同样. 分析: 在数据规模较小且不须要剪枝的情况下能够暴力(思路清晰代码简单),暴力一般分为 ...

  3. POJ 3187 杨辉三角+枚举排列 好题

    如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开 ...

  4. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  5. UVa 216 Getting in Line【枚举排列】

    题意:给出n个点的坐标,(2<=n<=8),现在要使得这n个点连通,问最小的距离的和 因为n很小,所以可以直接枚举这n个数的排列,算每一个排列的距离的和, 保留下距离和最小的那个排列就可以 ...

  6. UVa 729 The Hamming Distance Problem【枚举排列】

    题意:给出数组的长度n,给出h,表示这个数组里面含有h个1,求其所有的排列 用next_permutation就可以了 #include<iostream> #include<cst ...

  7. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...

  8. UVA - 10098 - Generating Fast (枚举排列)

    思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ...

  9. poj 2038 Team Rankings 枚举排列

    //poj 2038 //sep9 #include <iostream> #include <algorithm> using namespace std; char s[1 ...

  10. 枚举所有排列-STL中的next_permutation

    枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...

随机推荐

  1. Mr. Kitayuta's Colorful Graph CodeForces - 506D(均摊复杂度)

    Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the gr ...

  2. Windows 10中更新Anaconda和第三方包

    =============================== 作为专业的Python开发者,Anaconda包肯定很熟悉 下面总结一下Anaconda的升级和维护 步骤一: 打开cmd,切换到Ana ...

  3. JSP指令include和JSP动作元素include的区别

    include指令用于在JSP页面静态的包含一个文件,该文件可以是JSP页面.HTML页面.文本文件或者一段java代码.使用include指令的JSP页面在转换时,JSP容器会在其中插入所包含文件的 ...

  4. JavaScript中类似PHP的uniqid()方法

    JavaScript中类似PHP的uniqid()方法: function generateUIDNotMoreThan1million() { return ("0000" + ...

  5. 启用Win8/10(中文版/核心版/家庭版)中被阉割的远程桌面服务端

    Windows 8/8.1/10 标准版(中文版/核心版/家庭版)中取消了远程桌面服务端,想通过远程连接到自己的电脑就很麻烦了,第三方远程桌面速度又不理想(如TeamViewer).通过以下方法可让系 ...

  6. Ionic Tabs使用

    1. 创建Tabs相关页面 ionic g page tabs ionic g page TabOne ionic g page TabTwo ionic g page TabThree 2. 在ta ...

  7. C语言扩展动态内存报错:realloc(): invalid next size: 0x0000000002365010 ***

    晚上被这个内存扩展崩溃的问题折腾的有点崩溃,当答案揭晓的那一刻,恍然大悟,原来如此简单. 练习题目:输入一个字符串,根据字母进行排序,说白了就是一个简单的冒泡 #include <stdio.h ...

  8. springcould

     [Spring For All 社区周报] 「社区活动」(送书哦)Spring For All 第 1 期高手 QA 环节 — Spring Cloud 微服务实战http://spring4all ...

  9. python 典型文件结构

    #/usr/bin/env/ python #(1) 起始行 "this is a test module" #(2) 模块文档(文档字符串) import sys import ...

  10. 【python】python性能分析--待完善

    http://www.oschina.net/translate/python-performance-analysis http://blog.csdn.net/gzlaiyonghao/artic ...