【字母全排列】 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 ...
随机推荐
- Python虚拟环境的配置
Python中,配置虚拟环境主要是为了防止版本之间的冲突,我是这么理解的: 1.用虚拟环境可以在一个电脑中使用多个Python解释器以及扩展: 2.可以方便的在同一台电脑中使用多个版本的代码. 虚拟环 ...
- Java 集合 持有引用 & WeakHashMap
Java 集合 持有引用 & WeakHashMap @author ixenos 摘要:强引用.弱引用.WeakHashMap动态回收步骤 Reference引用对象 可获得的(reacha ...
- 二维离散平稳小波重构iswt2
clc,clear all,close all; load woman; [cA,cH,cV,cD]=swt2(X,2,'haar');%用haar小波基进行2尺度平稳小波分解 Y=iswt2(cA, ...
- 查看,添加和删除GIT配置的正确姿势
查看GIT所有配置的命令: git config --list 查看GIT全局配置的命令: git config --global --list 添加GIT全局配置(HTTPS代理) git conf ...
- document.onreadystatechange()来判断页面加载完
document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomething() { if(docum ...
- 杭电15题 The Cow Lexicon
Problem Description Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, eac ...
- iOS UITextView 根据输入text自适应高度
转载自:http://www.cnblogs.com/tmf-4838/p/5380495.html #import "ViewController.h" @interface V ...
- 2.Thread中的实例方法
(转自:http://www.cnblogs.com/xrq730/p/4851233.html) Thread类中的方法调用方式: 1.this.XXX 这种调用方式表示的线程是:线程实例本身 2. ...
- android 获取系统硬件信息
一,首先设置权限访问: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> ...
- Python logging模块使用记录
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...