7 POJ 1256 Anagram
2015-04-29 21:28
260 查看
给一个字符串包含大小写字符,规定'A'<'a'<'B'<'b'<...<'Z'<'z',求该字符串的全排列。
用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂。。
最开始用的set+next_permutation,太年轻,也超时了。。。
运用一个next_permutation()函数即可,<algorithm>头文件
注意要先将字符串sort一遍,然后next_permutation()也要把比较函数cmp传进去,原来都不知道可以三个参数的。。
用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂。。
最开始用的set+next_permutation,太年轻,也超时了。。。
用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂。。
最开始用的set+next_permutation,太年轻,也超时了。。。
运用一个next_permutation()函数即可,<algorithm>头文件
注意要先将字符串sort一遍,然后next_permutation()也要把比较函数cmp传进去,原来都不知道可以三个参数的。。
#include<cstdio> #include<set> #include<cstring> #include<algorithm> #include<string> #include<iostream> using namespace std; char s[20]; bool cmp(char a,char b) { if(a>='a'&&a<='z'&&b>='a'&&b<='z') return a<b; if(a>='A'&&a<='Z'&&b>='A'&&b<='Z') return a<b; if(abs(a-b)==32) return a<b; if(a>='A'&&a<='Z') a+=32; if(b>='A'&&b<='Z') b+=32; return a<b; } int main() { int T,len; scanf("%d",&T); while(T--) { scanf("%s",s); len=strlen(s); sort(s,s+len,cmp); do { puts(s); }while(next_permutation(s,s+len,cmp)); } return 0; }
用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂。。
最开始用的set+next_permutation,太年轻,也超时了。。。
相关文章推荐
- poj 1256 Anagram
- poj 1256 Anagram
- POJ 题目1256 Anagram(next_permutation)
- POJ1256 Anagram
- POJ 1256:Anagram
- Anagram poj 1256
- POJ - 1256 Anagram
- POJ 1256:Anagram
- POJ 1256 Anagram
- poj 1256 Anagram
- POJ 1256 Anagram(全排列问题)
- poj-1256 Anagram 全排列
- poj 1256 深搜全排列 Anagram
- 【STL】poj 1256 Anagram
- POJ 1256 Anagram
- poj 1256 Anagram—next_permutation的神奇应用
- poj 1256 Anagram
- (DFS)(全排列)(不用库函数)POJ 1256 Anagram
- poj 1256 Anagram
- POJ 1256 Anagram 笔记