【字母全排列】 poj 1256
深搜 注意与STL模版的去重函数唯一的区别就是有去重。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
int len;
],ss[];
];
bool cmp(char a,char b)
{
double t1=a,t2=b;
if(a>='A'&&a<='Z') t1+=31.5; //神来之笔,对于cmp的神级处理
if(t2>='A'&&t2<='Z') t2+=31.5;
return t1<t2;
}
void dfs(int x)
{
if(x==len)
{
;i<len;++i)
printf("%c",ch[i]);
printf("\n");
}
else
{
;i<len;i++) //以任意顺序开头
{
if(!visted[i])
{
ch[x]=ss[i];
visted[i]=;
dfs(x+);
visted[i]=; //另一进程需要更新dfs之前的值
<len&&ss[i+]==ss[i])i++; //去重
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(visted,,sizeof(visted));
scanf("%s",ss);
len=strlen(ss);
sort(ss,ss+len,cmp);
dfs();
}
;
}
网上看到的一段代码:
用的是STL的库函数next_permutation全排列
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(char a,char b)
{
if(tolower(a)==tolower(b))
return a<b;
else
return tolower(a)<tolower(b);
}
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
sort(str.begin(),str.end(),cmp);
do
{
cout<<str<<endl;
}
while(next_permutation(str.begin(),str.end(),cmp));
}
;
}
【字母全排列】 poj 1256的更多相关文章
- poj 1256 按一定顺序输出全排列(next_permutation)
Sample Input 3aAbabcacbaSample Output AabAbaaAbabAbAabaAabcacbbacbcacabcbaaabcaacbabacabcaacabacbaba ...
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 1256 Anagram(dfs)
题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...
- 简单的字母全排列问题—递归法和STL法
问题描述:求全由小写字母组成的不超过200个字符序列的全排列 如输入序列bbjd,排列结果为: bbdj bbjd bdbj bdjb bjbd bjdb dbbj dbjb djbb jbbd jb ...
- next_permutation,POJ(1256)
题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<... ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...
- POJ 1256:Anagram
Anagram Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18393 Accepted: 7484 Description ...
- WLLCM这五个字母全排列数目
经过训练的话一眼看出来是5!/2!:我想的是先排WLCM那么是4!,5个位置,由于L左右两边的位置其实是一样的(再插入的还是L),索以结果是4*4!,这样重复了,看下图. ...
- POJ 1256
//#include "stdafx.h" #include <stdio.h> #include <string.h> #define N_MAX 14 ...
随机推荐
- Eclipse版本区别
各版本的区别: Eclipse IDE for Java Developers 是Eclipse的platform加上JDT插件,用来java开发的 Eclipse IDE for Java EE D ...
- CSS3的基础知识点
面临找工作之际,又将CSS3的基础知识撸了一把,做了相应的笔记,主要是方便自己查阅,参考的是W3C的知识. 1.CSS背景 (1).background-size 属性 background-s ...
- 2.Math对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- web.py 学习(二)Worker
Rocket Server 启动一个线程监听客户端的连接,收到连接将连接放置到队列中.线程池中的Worker会以这个连接进行初始化.Rocket中Worker的基类是: class Worker(Th ...
- openwrt 汉化
make menuconfig 添加luci LuCI--->Collections----- <*> luci 添加luci的中文语言包 LuCI--->Translatio ...
- HDU 1258 Sum It Up(DFS)
题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...
- 理解 Objective-C 的 ARC
英文原文:Understanding Automatic Reference Counting in Objective-C 自动引用计数(Automatic Reference Counting, ...
- ubuntu12.04安装openjdk-7
编译androidL需要使用openjdk-7:使用sudo apt-get install openjdk-7-jdk会出现错误信息. 网上差了一些方法,可以通过更新软件源的方法解决,这里使用了 # ...
- 安卓手机微信页面position: fixed位置错误
今天做项目的时候发现动用position: fixed做弹窗时,用margin-top:50%这样外边距来响应式的控制位置时,在微信里打开页面的弹窗,弹窗在手机上显示的位置和实际上在手机上的位置不一样 ...
- 如何成为出色的IT项目经理:成功的五个关键因素
“出色”的IT 项目经理的定义不是一成不变的.随着经济和商业因素的改变,项目经理的角色进行调整以适应新的需求,迎接新的挑战. 除了一般的困惑之外,还有一种看法就是,在组织中,不同的人对于项目经理的看法 ...