poj 1256 Anagram(dfs)
题目链接:http://poj.org/problem?id=1256
思路分析:该题为含有重复元素的全排列问题;由于题目中字符长度较小,采用暴力法解决。
代码如下:
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = ;
char P[MAX_N], A[MAX_N]; char * SortAlp(char P[], int n)
{
int Low[MAX_N], Upper[MAX_N];
int LowLen, UpperLen; LowLen = UpperLen = ;
for (int i = ; i < n; ++ i)
{
if ('A' <= P[i] && P[i] <= 'Z')
Upper[UpperLen++] = P[i];
else
Low[LowLen++] = P[i];
}
sort(Low, Low + LowLen);
sort(Upper, Upper + UpperLen); int Index_L, Index_U;
Index_L = Index_U = ;
for (int j = ; j < n; ++j)
{
if (Upper[Index_U] - 'A' + 'a' <= Low[Index_L]
&& Index_U < UpperLen)
P[j] = Upper[Index_U++];
else
P[j] = Low[Index_L++];
}
return P;
} void PrintPermutation(int n, char P[], char A[], int cur)
{
int i, j; if (cur == n)
{
for (i = ; i < n; ++i)
printf("%c", A[i]);
printf("\n");
}
else
{
for (i = ; i < n; ++i)
{
if (!i || P[i] != P[i-])
{
int c1 = , c2 = ; for (j = ; j < cur; ++j)
if (A[j] == P[i]) c1++;
for (j = ; j < n; ++j)
if (P[i] == P[j]) c2++; if (c1 < c2)
{
A[cur] = P[i];
PrintPermutation(n, P, A, cur + );
}
}
}
}
} int main()
{
int n;
char P[MAX_N]; cin >> n;
for (int i = ; i < n; ++i)
{
cin >> P;
SortAlp(P, strlen(P));
PrintPermutation(strlen(P), P, A, );
}
return ;
}
poj 1256 Anagram(dfs)的更多相关文章
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- POJ 1256:Anagram
Anagram Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18393 Accepted: 7484 Description ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- poj 1816 (Trie + dfs)
题目链接:http://poj.org/problem?id=1816 思路:建好一颗Trie树,由于给定的模式串可能会重复,在原来定义的结构体中需要增加一个vector用来记录那些以该节点为结尾的字 ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- POJ 1321 简单dfs
1.POJ 1321 棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...
随机推荐
- 有感PMI Exam Dev Workshop
有幸參加了PMI协会在上海举办的PMI Exam Development Workshop活动.这是PMI协会第二次在中国举办此活动,上一次是2009年北京. 我第一次參加,感觉收获非常多. 我们知道 ...
- SQL Server索引进阶:第二级,深入非聚集索引
原文地址: Stairway to SQL Server Indexes: Level 2, Deeper into Nonclustered Indexes 本文是SQL Server索引进阶系列( ...
- Mybaits入门之起航
前言 Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下. 学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而 ...
- 刚开始学HTML自己做的,求大神些多多指教。
!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> ...
- 初步认识ExtJS
最近因为项目,需要去学习ExtJS的相关内容. 现在对于ExtJS完全是小白一枚. 目前使用的是ExtJS4.2的版本,官网上现在最新版本是6的. 第一个方法:Ext.onReady() Ext.on ...
- Android应用开发基础篇(7)-----BroadcastReceiver
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/22/2363644.html 一.概述 BroadcastReceiver,意思就是广播信息接收 ...
- If We Were a Child Again
Description The Problem The first project for the poor student was to make a calculator that can jus ...
- 最大流之sap算法
若有向图G = (V , E)满足下列条件: 1.有且仅有一个顶点S,它的入度为 0 ,这个顶点称为源点. 2.有且仅有一个顶点T,它的出度为 0 ,这个顶点称为汇点. 3.每一条弧都有一个非负数,叫 ...
- 建造者模式->代码示例
<?php interface Builder{ public function head(); public function body(); public function foot(); ...
- android五种布局模式
Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对 ...