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的更多相关文章

  1. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  2. 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 ...

  3. 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 ...

  4. 【09_242】Valid Anagram

    Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...

  5. 【leetcode❤python】242. Valid Anagram

    class Solution(object):    def isAnagram(self, s, t):        if sorted(list(s.lower()))==sorted(list ...

  6. 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 ...

  7. (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 ...

  8. 【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 ...

  9. 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, ...

  10. 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 = & ...

随机推荐

  1. php session 和cookie

    先简单的说明下session和cookie的区别. 1.session存放在服务器的文件中,或者是内存中.而cookie存在客服端. 2.session比cookie安全 3.session存放在服务 ...

  2. JXL组件生成报表报错(二)

    JXL组件生成报表 1.具体报错如下: usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonam ...

  3. 无法连接MySQL

    1.错误描述 Error No. 2003 Can't connect to MySQL Server on 'localhost' (10061) 2.错误原因       打开任务管理器,关闭了m ...

  4. Caused by: java.lang.ClassNotFoundException: flex.messaging.io.BeanProxy

    1.错误描述 2014-7-13 1:34:46 org.apache.catalina.startup.HostConfig undeploy 信息: Undeploying context [/b ...

  5. 最新数组方法(包括es6)

    整理目前所用过的数组方法,学习了新增的es6方法. 1 arr.push() 从后面添加元素,返回值为添加完后的数组的长度 let arr = [1,2,3,4,5] console.log(arr. ...

  6. 【编程笔记】Unity3D语言的类型系统--C#的类型系统

    几乎所有的编程语言都有自己的类型系统. 而编程语言更是常常按照其类型系统而被分为强类型语言/弱类型语言.安全类型语言/不安全类型语言.静态类型语言/动态类型语言等. 而C#的类型系统是静态.安全,并且 ...

  7. 【BZOJ3924】幻想乡战略游戏(动态点分治)

    [BZOJ3924]幻想乡战略游戏(动态点分治) 题面 权限题...(穷死我了) 洛谷 题解 考虑不修改 发现一个贪心的做法 假设当前放在当前位置 如果它有一个子树的兵的总数大于总数的一半 那么,放到 ...

  8. 300行Kotlin代码实现的区块链

    使用Kotlin实现的区块链基本逻辑! 源码地址 GitHub仓库 启动方式 启动参数添加 -Dserver.port=8080,启动不同的端口,模拟不同的节点. 假设目前启动了8080和8081两个 ...

  9. Java求最大公约数和最小公倍数

    最大公约数(Greatest Common Divisor(GCD)) 基本概念 最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为(a,b),同样的 ...

  10. python数据类型——数字类型

    Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) 数字类型(Number): ...