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 = & ...
随机推荐
- Android app security安全问题总结
数据泄漏 本地文件敏感数据不能明文保存,不能伪加密(Base64,自定义算法等) android:allowbackup=false. 防止 adb backup 导出数据 Activity inte ...
- 错误代码: 1054 Unknown column 'course' in 'field list'
1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:SELECT stu_name, course, score FROM t_ ...
- Ajax前后台交互函数
function AJAX(obj){ //做网络请求的时候参数以对象的形式传递进来 //我们规定obj里面包含属性:url, //请求方式type, //date前端给后端传递的参数 //回调函数s ...
- 应对不同格式 轻松转换PDF、WORD、PPT、TXT常用文件
PDF.WORD.PPT.TXT,不同格式的文件是不是弄得你眼花缭乱?如何巧妙地将它们相互转换?你不会还在键盘上傻傻地一个字一个字敲吧?教你不同文件格式间的转换方式,轻松几键便能大功告成.职场之上,你 ...
- 改造 vue-cli 脚手架
改造 vue-cli 脚手架 注意,这里使用的 vue-cli 的版本为 2.8.2,不同版本的 vue-cli 可能会有些许不一样. 一.配置多页面 项目开发目录: 需要注意一点,每个页面的文件夹命 ...
- Css3关键帧动画
@keyframes设置动画规则,可以理解为一个剧本: 1.name动画的名字必需填写 2.可以使用百分比0%-100%或者from...to...必填内容: 3.需要变化css的样式:必需: ani ...
- Session和Cookie总结
一.Session和Cookie 1.Cookie 1.cookie创建于服务器,保存于浏览器,保存了特定网站操作记录和资料凭证的信息. 2.未设置cookie期限的时候,默认是关闭浏览器后cooki ...
- 用CSS画出好玩的图形
1.上三角 #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px so ...
- Vue2.0 demo:百度百聘第三方web客户端
github地址:https://github.com/axel10/baipin_vue 项目地址:https://vcollection.org/baipin/ 官方的百度百聘客户端存在翻页时过滤 ...
- 从头编写asp.net core 2.0 web api 基础框架 (5) + 使用Identity Server 4建立Authorization Server (7) 可运行前后台源码
前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...