Anagram
Anagram poj-1256
题目大意:给你n个字符串,求每一个字符串所有字符的全排列,按照顺序输出所有全排列。
注释:每一个字符长度小于13,且字符排序的顺序是:A<a<B<b<C<c<...<Y<y<Z<z
想法:哇塞,我是读完了题,完全码完了之后才发现顺序不是字典序的... ...悲催。这题是一道挺有意思的全排列的题,这题其实需要我们去掉重复的字符。不然的话,输出的全排列会有明显的重复,我们在这里用结构体离散化整个字符串,将每个字符存进一个结构体,同样的,结构体里还有这个字符的出现次数。dis[s[i]-'0']表示s[i]这个字符在dis[s[i]-'0']个结构体里。然后就是最经典的这道题的好玩儿的地方——compare函数(cmp)。附上丑陋的cmp函数
struct Node
{
char change;//change表示这个结构体所代表的字符
int sum;//sum表示这个字符所出现的次数
}f[];
bool cmp(Node x, Node y)
{
char a=x.change;
char b=y.change;
if(a<='Z'&&b<='Z')
return a<b;
else if(a>='a'&&b>='a')
return a<b;
else if(a>='a'&&b<='Z')
return a-'a'<b-'A';
else if(b>='a'&&a<='Z')
return a-'A'<=b-'a';
}
然后,就是一些“小”问题了,都是细节啊!!
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char ans[];
int dis[];
int k;
int cnt;
char s[];
struct Node
{
char change;//change表示这个结构体所代表的字符
int sum;//sum表示这个字符所出现的次数
}f[];
bool cmp(Node x, Node y)
{
char a=x.change;
char b=y.change;
if(a<='Z'&&b<='Z')
return a<b;
else if(a>='a'&&b>='a')
return a<b;
else if(a>='a'&&b<='Z')
return a-'a'<b-'A';
else if(b>='a'&&a<='Z')
return a-'A'<=b-'a';
}
void dfs(int a)//表示当前正在填充全排列其中一个排列的第a个字符
{
if(a==k+)//退出条件
{
printf("%s\n",ans+);//ans数组存的是生成的全排列
return;
}
for(int i=;i<=cnt;i++)
{
if(f[i].sum)
{
ans[a]=f[i].change;
f[i].sum--;
dfs(a+);
f[i].sum++;//回溯!
}
}
}
int main()
{
int cases;
scanf("%d",&cases);
while(cases--)
{
memset(dis,,sizeof(dis));
memset(ans,,sizeof(ans));
cnt=;
for(int i=;i<=;i++) f[i].sum=;
scanf("%s",s+);
k=strlen(s+);
for(int i=;i<=k;i++)
{
if(!dis[s[i]-''])
{
dis[s[i]-'']=cnt+;
cnt++;
f[cnt].change=s[i];
}
f[dis[s[i]-'']].sum++;
}
sort(f+,f+cnt+,cmp);
dfs();
}
return ;
}
小结:注意dis数组表示的是什么,我一直都给赋值成了1... ...
Anagram的更多相关文章
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
- 242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- (easy)LeetCode 242.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- 【LeetCode】242 - Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- Java [Leetcode 242]Valid Anagram
题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- Leetcode 242. Valid Anagram(有效的变位词)
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
随机推荐
- UTF-8 的BOM带来的麻烦
UTF-8 的BOM带来的麻烦 工作需要我用程序生成一个html文件. 由于服务器端使用apache+Tomcat来执行html和jsp文件. 开始生成html文件放在apache目录下,页面无法默认 ...
- Unity开发之实现更换鼠标图片
在玩游戏的时候,感觉游戏里的鼠标图片特酷炫,23333,今天我就总结了两种方法! 我是做Unity开发的,所以方法仅针对于Unity平台........ 方法如下: 1.Unity客户端直接更改,步骤 ...
- 畅通工程 HDU - 1863
省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道 ...
- 异常-----freemarker.core.ParseException: Encountered
1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column 12 in myself.ftl ...
- 教你如何制作网页上的友情链接--JavaScript基础
大部分网站的首页都有友情链接的功能,此功能可通过location对象的href属性来实现…… href属性:设置或检索完整的url字符串 1."友情链接制作"示例代码: <! ...
- Java8 日期/时间(Date Time)使用简介
特别说明: LocalDateTime 为日期时间的计算提供了很大的方便, 在构造对象/运算/toString等方便都非常便利. 3个常用的类: java.time.LocalDateTime; ja ...
- Mybatis常见面试题
Mybatis常见面试题 #{}和${}的区别是什么? #{}和${}的区别是什么? 在Mybatis中,有两种占位符 #{}解析传递进来的参数数据 ${}对传递进来的参数原样拼接在SQL中 #{}是 ...
- 【编程笔记】Unity3D语言的类型系统--C#的类型系统
几乎所有的编程语言都有自己的类型系统. 而编程语言更是常常按照其类型系统而被分为强类型语言/弱类型语言.安全类型语言/不安全类型语言.静态类型语言/动态类型语言等. 而C#的类型系统是静态.安全,并且 ...
- 【NOI2008】假面舞会(图论,搜索)
题面 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一个自己喜欢的面 具.每个面具都有一个编号 ...
- [HNOI2014]米特运输
显然知道一个节点就可以推出整棵树 然而直接乘会爆longlong 所以考虑取log 最后排序算众数即可 # include <stdio.h> # include <stdlib.h ...