【字母全排列】 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 ...
随机推荐
- Linux入门(六)ubuntu下vim编辑器安装与使用
原文链接:http://jingyan.baidu.com/article/219f4bf7d03187de452d3862.html 一.安装 vim 控制台版本的vim随ubuntu一起发行,因此 ...
- 苹果dock效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ggplot2 scale相关设置2—时间设置
在scale设置中,常用的日期方面的设置函数包括: scale_x_date(),scale_y_date(),scale_x_datetime(),scale_y_datetime() 接下来, ...
- shell初学
超简单的一段shell代码,查看电脑属性,删除无效安装包,查看天气.FYI #!/bin/bash echo -e '\n' echo "Hello,`whoami`" echo ...
- git config and options core.bare hard
In Lynda course Building a Web Interface with React.js 003 Using the exercises > git clone --bare ...
- 关于windows线程同步的四种方法
#include "stdafx.h" #include "iostream" #include "list" #include " ...
- AIR文件操作:使用文件对象操作文件和目录 .
来源:http://blog.csdn.net/zdingxin/article/details/6635376 在AIR中可以方便的对本地文件操作,不过上次做了个项目,发现还是有不少不方便的地方,比 ...
- asp.net URL DES加密 什在URL中的使用
DES 加密 有时会有 + = 这些特殊字符串 Server.UrlDecode(Request["UserName"]) //能解决 = 号,但是 +号会变成空格 HttpUt ...
- JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...
- 安装gensim
安装了一天的gensim,其中因为版本不一致等等各种问题纠结了好久,现记录如下: 正确安装方式: 1. 安装python2.7 2. 下载Python Extension Packages对应版本的n ...