简单的字母全排列问题—递归法和STL法
#include <stdio.h> int t[];
char s[];
int n = ; void permutation(int i)
{
int k; // a~z的ASCII码在97到122之间
for(k = ; k < ; k++)
{
if(t[k])
{
t[s[i] = k]--;
permutation(i + );
t[k]++;
}
} // 只有n个字母全部排好了才输出
n - i || puts(s);
} int main()
{
int i; puts("Please Input letter sequence: ");
gets(s); // t[]记录每个字母的出现频率
// n为待排序列的长度
for(i = ; s[i] != '\0'; i++)
{
t[s[i]]++;
n++;
} puts("Permutation result: ");
permutation(); return ;
}
运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAAEXCAIAAABH7JRTAAAYOUlEQVR4nO3dd3fbWHoGcHy0TbI5my3Z4rFntkxmZ8aT3Wwd725ykuxHybjbcrcpqlKdkkg19t57JwAWiaSKTZ/8cSmKIohLgGV4bT+/8545EHB5cQHiGVAgYHG7gcJekN8PiweRiilaMcWqptihKXZoilYPIuW9kLgT4A3ewpY7o3cm123xNWt0xRJZMYdXTKFVc2TVEl2zxjZsCb0rue3OGLw5o6+w6y/sBfi9kLAX5Hf8RaM3v+1Ob7qSG474ui22aomsWsKrlsiaNbpmi284Epuu1LY7a/AWdvz8bkDYC4p7odJeuLwXLu+FSruh0m6wtBsUd4PiznntBsXdkLgTFI1B0RgUdwLiTkA0+i/K4Be3/aLBLxoC4nZA3Pa3assvbvpapfeKG15xw8Ovu/l1N7/qKq66iiuOwrI9t2TNLloyC6b0vCk9f5Ce3U/O7ifnTZkFU2bBktfZ8ssOfsUtrLjFNU951VNadYsrbmHZxescxQVbbt6SmTWltAep6b2kZjep2U2+2iUTKc1ueno/qzXlZk35OUtx3sYvOASdQ9S5xEXnRelIuUpLrpLOKS46xEWHuGDn523FOWth1pKfseRmLPkZa15rKcxY8lprYcZanLEVtRZ+xsprbcKMTZy1iTM2ccYuztpLs45WzdDL3iqtXdTaRa2tVdM2cdoqaq2i1ipclE3Q2kStXZwh5RBnyFqc8uUozTrEWbs4axdnbOK0ldeY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TEZHSs/wfCkAi3qmlKCcG9vTPWdS2gOAakgpAOuQUgDWzXqQUgC2daVULlpdsZSmsSu0lPYAoM6sp06KnlIAmJg5T2PO02inlMPzpQCskaYUANhyntIGUgrAKJLSOaQUgFlzXqQUgG3tlEaRUgA2zXmPSVClKe15mbfr8q/c1WBcJQYYmTnv8Zz3WHlKpfMVNgOAAbVS6lXxiRcpBfhWnaf0OCrg+VIAJs1fSunfOxdJf/+UTitshqACDK4jpa+RUgAWzfuO573H80gpALPmfSckqEgpAKMup5T25Jr+XOeirgnpa7vmA4Bq874TUn1TCgCT0ZVSDs+XArBGmlIAYAtSCsA6pBSAdQtIKQDjkFIA1i34ThYk38R8OJd5P5DNVA57g0UL/tMF/8mC5JsYsvRDeM++hW1kbR9SxvMhvOPvngX/KQlqVERK38n+1WJtPNDfeUpP6Snt/GTY9TbLfTxWO19Oz/b6y6TtpdP0/uWmh+xfL0HfLoVd9Z2vZKVKxkNv3HMnqN0u6E8upZQ3pnOR3LuldloJhf2oXYWqbRmmfyXboqSTIfczfXUKN3aA9cLg6OfSTvpznOTI7tTVXq4f5SPs2b/Co1DJuiiD77tdyvunzBy4E0o/CvdP3xWp7Z+y32BwalOqZKaSBgrfwiGPQlUBUPjCgfsfeSeUfhTun74rUtu/km0B1UaY0nG8o4P1o+9A71+uq3H0/23uB0r/PcfTc0WjXS8Mriulcscf5bhU+xJKVz11Nu55hMl1pWQVXSORTlM6GWwTpDPVdqKkH/r+6duPkv1MbybXWLoK6E96Ln0/jPsgwEGmClI6lPcspeM+AnCEDQYpHcp7llJgE1I6lEWkFIBxi4GzRaQUgGW64BkJKlIKwCikFIB1uuCZLnCmC5zFkFIANumCZ6Ri4hukFIBFSCkA6y5SWkJKAZiElAKwDikFYB1SCsA6pBSAdUgpAOt0wddIKQDTkFIA1umCr0lQkVIARiGlAKxDSgFYd57S10gpAJv+HynCma3+7V3dAAAAAElFTkSuQmCC" alt="" />
方法二:STL法
C++的STL有一个函数可以方便地生成全排列,这就是next_permutation
在C++ Reference中查看了一下next_permutation的函数声明:
#include <algorithm>
bool next_permutation( iterator start, iterator end );
The next_permutation()
function attempts to transform the given range of elements [start,end)
into the next lexicographically greater permutation of elements. If it
succeeds, it returns true, otherwise, it returns false.
从说明中可以看到 next_permutation 的返回值是布尔类型。按照提示写了一个标准C++程序:
其中用到了 sort 函数,如果不先排序,则完成不了全排列。
#include <iostream>
#include <string>
#include <algorithm> using namespace std; int main()
{
string str; cout << "Please Input letter sequence: \n";
cin >> str; // 必须先排序
sort(str.begin(), str.end());
cout << str << endl; cout << "Permutation result: \n"; while(next_permutation(str.begin(), str.end()))
{
cout << str << endl;
} return ;
}
运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVMAAAEdCAIAAAA+exM/AAAZ00lEQVR4nO3d928bWWIHcP5dCXKXS8Uhhyt75yJbTnJZ27Lsvdtdd/twSA4BDpeCZIHgcOv12urrpmZZliVRIqliiVRlGbGTYu+9U9IQ+YEURXEKh1ThiPP94MEYP75582Y4Xw05RRKt2DIbzuymK69076g8u2rvrtq7V1F2VZ5DReneVRan3bsq967Ks6v07Kg8Oyp3qag9h4qmNJFXe/Jq96GiOlyUrGXTVSobzlJZd+bXHbk1R27VnluxZRW2tHw7Lbemlq3JZUty6aCkSsWaWrKmlrczcltWbsspbPlVR37NubPm2ttw7a27yHUPue4m193kuudges1FrrnIVRepcOzJ7TtyW15uyy9Zs4vm9LwpOWeIy/QxqT4q0Ucl2sisLirRxyT6mEQXn9XGZraiYiIyTYSnNeFpdWhKHfygCk6qglPq0JQmPE1ExFvRGV1i1pCWmjJSU0ZqykqMpTJrzM4aMzPGrNiQFutS09rk1FZigoi9U4fHNkOj6/6RtWLxjaz6hle8QwrPoNz1Ru58tWR/+dH2YsE6MGfukxh7Z3Q94q2uKU33NNEj3uoW63pm9H2zxj6pqV9mHpiz9ktNfRJDr1jXLd7qFhNd00TXNNE1RXSLiR6xtndW3ycx9s7oe8Tarmni+aTm2aT62Qf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ekHTvFyLvlyL6n3Z8y30N3ZOes/Dng1n3uBmfHAjbvTnWuAvap70AQcHNGgdI6rkiCppCuTbOvFXtAEE4606/VadNgfzl28+avZYAOC0jBHZMSJjCe4g+QACguQDCNFbIvOWyJiRfABB2f+ev3MJZ/gAhGNYmRpWpkyBfNsNJB9AMAY3EoMbida4ng8AXL1aj71aixl8uQsdrXMPHwDU8GI1+mI1qvdlz7XQ3bsAUEO/ItyvCOu8mXPX7jV7LABwWvqWw33L4S1P+udX7zZ7LABwWro+BroWA4Q7XXw+HwAE4em89+m8V+NM/fSXt5s9FgA4LX+adf5p1qlyJIq/hw8ABOGrD9tffdje2I79w5VfN3ssAHBa/uOt8Q9vjavmyA8vfdbssQDAafndG+J3bwi5Mfh3F282eywAcFp+M7D5m4HNjzr/X5+70eyxAMBpude9cq97dWHL94NfXG/2WADgtHz5rfzLZ4o5wvv9T641eywAcFo+/2b586fLMo3ne59cbfZYAOC0/Prp0uffLMs0nu/9DMkHEIwvvl3+4lv5nMaDT/sAAnL7ueLO85V5wvuXn+AMH4BgPOhZf9C7vqj1/eAXHc0eCwCckv8H5qb0IFXTrOwAAAAASUVORK5CYII=" alt="" />
在使用稍大数据测试的时候,发现标准C++的效率很差,换成C函数写一下,效率提升了许多倍,比如我用字符序列“aabbddef”作为测试,上面C++代码用了6s,而下面的C代码只用了0.918s。所以,平时写代码时能用C就用C。
#include <stdio.h>
#include <algorithm> using namespace std; int main()
{
char str[];
int len; puts("Please Input letter sequence: ");
gets(str); len = strlen(str); // 必须先排序
sort(str, str + len); puts("Permutation result: ");
puts(str); while(next_permutation(str, str + len))
{
puts(str);
} return ;
}
简单的字母全排列问题—递归法和STL法的更多相关文章
- codevs http://www.codevs.cn/problem/?problemset_id=1 循环、递归、stl复习题
12.10高一练习题 1.要求: 这周回顾复习的内容是循环.递归.stl. 不要因为题目简单就放弃不做,现在就是练习基础. 2.练习题: (1)循环 题目解析与代码见随笔分类 NOI题库 htt ...
- n个整数全排列的递归实现(C++)
全排列是很经常使用的一个小算法,以下是n个整数全排列的递归实现,使用的是C++ #include <iostream> using namespace std; int n = 0; vo ...
- 集成学习的不二法门bagging、boosting和三大法宝<结合策略>平均法,投票法和学习法(stacking)
单个学习器要么容易欠拟合要么容易过拟合,为了获得泛化性能优良的学习器,可以训练多个个体学习器,通过一定的结合策略,最终形成一个强学习器.这种集成多个个体学习器的方法称为集成学习(ensemble le ...
- dp的刷表法和填表法
dp的刷表法和填表法 参考: 动态规划刷表法 - acmer_xue的博客 - CSDN博客http://blog.csdn.net/qq_30241305/article/details/52198 ...
- TINY语言采用递归下降分析法编写语法分析程序
目录 自顶向下分析方法 TINY文法 消左提左.构造first follow 基本思想 python构造源码 运行结果 参考来源:聊聊编译原理(二) - 语法分析 自顶向下分析方法 自顶向下分析方法: ...
- 全排列问题(递归&非递归&STL函数)
问题描述: 打印输出1-9的所有全排序列,或者打印输出a-d的全排列. 思路分析: 将每个元素放到余下n-1个元素组成的队列最前方,然后对剩余元素进行全排列,依次递归下去. 比如:1 2 3 为例首先 ...
- 生成n个数的全排列【递归、回溯】
下面讨论的是n个互不相同的数形成的不同排列的个数.毕竟,假如n个数当中有相同的数,那n!种排列当中肯定会有一些排列是重复的,这样就是一个不一样的问题了. /*===================== ...
- 全排列算法--递归实现(Java)
求一个n阶行列式,一个比较简单的方法就是使用全排列的方法,那么简述以下全排列算法的递归实现. 首先举一个简单的例子说明算法的原理,既然是递归,首先说明一下出口条件.以[1, 2]为例 首先展示一下主要 ...
- iOS简单排序--字母排序、NSDictionary排序
// 数组用系统方法compare做字母的简单排序 NSArray *oldArray = @[@"bac",@"bzd",@"azc",@ ...
随机推荐
- 适配android和iOS上position:absolute和input问题
//适配android上absolute和input的问题var oHeight = $(document).height(); //屏幕当前的高度$(window).resize(function( ...
- redis之hello
1.创建一个maven工程 2.进入redis官网 https://github.com/xetorthio/jedis 3.找到 <!--导入到pom.xml文件中--><depe ...
- concurrent.future
concurrent.future module provides a high-level interface for asynchronously executing callables. Bas ...
- Codeforces812A Sagheer and Crossroads 2017-06-02 20:41 139人阅读 评论(0) 收藏
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
- hdu 5074 相邻数和最大dp
http://acm.hdu.edu.cn/showproblem.php?pid=5074 给定一个序列 有些位数未知,给你所有两个数连续所得到的能量,问你怎么安排数字使得总能量最大 二维dp,dp ...
- bootstrap1.3
<html> <head> <meta charset="UTF-8"> <title></title> ...
- 分形之龙形曲线(Dragon Curve)
龙形曲线(Dragon Curve)又叫分形龙,是一种自相似碎形曲线的统称,因形似龙的蜿蜒盘曲而得名. 一种简单的生成分形龙的方式是:拿着一条细长的纸带,把它朝下的一头拿上来,与上面的一头并到一起.用 ...
- Log4Net记录到数据库
WinForm下Log4Net的配置 Log4Net 组件下载地址 https://download.csdn.net/download/zgx123zgx123zg/10470986 configS ...
- 迁移桌面程序到MS Store(7)——APPX + Service
本篇我们以一个Sample工程,来说明如何把一个常见结构的desktop application,转制成APPX并在MS Store提供下载. 之前的篇章中,我们已经介绍了一些内容,包括如何通过Vis ...
- 【mock】后端不来过夜半,闲敲mock落灯花 (mockjs+Vuex+Vue实战)
mock的由来[假] 赵师秀:南宋时期的一位前端工程师 诗词背景:在一个梅雨纷纷的夜晚,正处于项目编码阶段,书童却带来消息:写后端的李秀才在几个时辰前就赶往临安度假去了,!此时手头仅有一个简单 ...