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 = & ...
随机推荐
- MySql获取所有表名
如何获取MySql中所有表的的表名? sql语句是:show tables 返回结果如下: 不仅仅返回了所有的表名,更返回了视图的名字.
- json数据的转义
{ "DSGA": { "approval": "qatest_nj" }, "applydetailId": &quo ...
- js中的回调函数的理解
一,常见的但是不是特别注意的回调方法. 1.1,ajax $.ajax({ url:"test.json", type: "GET", data: {usern ...
- tp5时间戳转日期的方法
{:date('Y-m-d H:i',$vo['create_time'])}
- Project入门学习
Microsoft Office Project制定项目计划时,针对每项任务是可以分配具体的资源的,比如由某个人完成某项任务,或者把某项设备分配到某项任务,这样便于项目的管理和人员.设备的安排及有效利 ...
- Windows Developer Day - Windows AI Platform
本次 Windows Developer Day,最值得期待的莫过于 Windows AI Platform 了,可以说是千呼万唤始出来.观看直播的开发者们,留言最多的也是 Windows AI Pl ...
- ORA-01940: cannot drop a user that is currently connected解决方法
我们在删除数据库用户时候会碰到如下错误 SQL> DROP USER sys_xj cascade; DROP USER sys_xj cascade*ERROR at line 1:ORA-0 ...
- 下一代 Android
据闻,Android M 是下一代 Android 的开发代号.那么,对于 M,Google 正在实验着什么? 指纹识别 根据此前的消息,Nexus 6 据称原本是包含指纹识别传感器的,但后来在开发过 ...
- 【洛谷2744 】【CJOJ1804】[USACO5.3]量取牛奶Milk Measuring
题面 Description 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少 ...
- .net白盒测试
很久没写博客了,刚好这段时间空闲,做点记录 前提:最近部门需要白盒测试的工具,在网上也搜索了很多资料,国内很少有类似的资料(很少公司.net代码进行白盒测试),最后在国外(FQ)网站查找到了部分资料 ...