Anagram——[枚举全排列]
2016-03-15 23:28
281 查看
预备知识:
1.求0—n个数全排列的算法:
void print_permutation(int n,int *A,int cur){ if(cur==n){ for(int i=0;i<cur;i++) cout<<A[i]<<" "; cout<<endl; } else for(int i=1;i<=n;i++){ int ok=1; for(int j=0;ok&&j<cur;j++) if(A[j]==i) ok=0; if(ok){ A[cur]=i; print_permutation(n,A,cur+1); } } }
2.另一种求数组A全排列的方法是使用头文件<algorithm>中的next_permutation()函数,使用之前要排序。
用法如下:
sort(p,p+n); do{ for(int i=0;i<n;i++)cout<<p[i]<<endl; }while(next_permutation(p,p+n));
下面进入正题:
Description
You are to write a program that has to generate all possible words from a given set of letters.Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba".
In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.
Input
The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.Output
For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.Sample Input
3 aAb abc acba
Sample Output
Aab Aba aAb abA bAa baA abc acb bac bca cab cba aabc aacb abac abca acab acba baac baca bcaa caab caba cbaa
Hint
An upper case letter goes before the corresponding lower case letter.So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'. 解题思路: 一道常规的全排列问题,解决思路就是对每个单词进行全排列,然后按设定的优先顺序排序后输出。 注意: 1.“For each word in the input”这句话说明对每一个给定的单词都进行全排列输出,并指明了输出的次序由输入次序决定。 2.注意比较函数的严密性 代码参考如下:
#include <iostream> #include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <time.h> using namespace std; #define clock__ (double(clock())/CLOCKS_PER_SEC) const int maxs=13; struct word{ char p[maxs+3]; }; bool cmp_letter(const char& a,const char& b){ if((tolower(a)<tolower(b))||(tolower(a)==tolower(b)&&isupper(a)&&islower(b))) return true; return false; } bool cmp_word(const word &a,const word &b){ int len1=strlen(a.p); int len2=strlen(b.p); if(len1<len2) return true; if(len1==len2){ for(int i=0;i<len1;i++) if(cmp_letter(a.p[i], b.p[i])) return true; else if(cmp_letter(b.p[i], a.p[i])) return false ; } return false; } int n; word p; int main() { scanf("%d",&n); while(n--){ vector<word> q; scanf("%s",p.p); int len=strlen(p.p); sort(p.p, p.p+len); do{ q.push_back(p); }while(next_permutation(p.p, p.p+len)); sort(q.begin(), q.end(),cmp_word); for(int i=0;i<q.size();i++) printf("%s\n",q[i].p); } //printf("time : %lf\n",clock__); return 0; }
相关文章推荐
- 购买阿里云服务器到服务器环境搭建(CentOS7.0:jdk+mysql+tomcat)
- PHP实现一个日历
- OpenGL碰撞检测与模型运动
- 图像处理之中值滤波介绍及C实现
- 一口气从CSS讲到Servlet再到JSP、Struts2,清蒸JavaWeb的前前后后。
- strspn&strcspn
- 部署Maven项目无法找到Maven管理的Jar包
- 活用 巧用数据结构
- 选择排序和冒泡排序(JAVA)
- 信管师培训之第二节课作业(系统集成知识)
- java基础(二)之数据类型
- iOS企业开发者计划注册流程,诸多埋伏~
- Latent dirichlet allocation note
- Springframework和Hibernate版本对应关系
- POJ3189 Steady Cow Assignment(最大流)
- 字符编码
- SQL基础-->数据库事务(TRANSACTION)
- 《零年:1945 现代世界诞生的时刻》读后感
- centos7编译替换内核
- 上传附件插件的使用