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. const char *p;和char * const p的区别

    const char *p;       const修饰*p,所以*p是一个常量,不可修改. char* const p; const修饰p,所以指针p是一个常量,不可修改. #include< ...

  2. 获取Filter的三种途径

    一.通过CLSID [cpp] view plaincopyprint? IBaseFilter *pF = 0; HRESULT hr = CoCreateInstance(clsid, 0, CL ...

  3. TypeError: Error #1034: 强制转换类型失败:无法将 mx.controls::DataGrid@9a7c0a1 转换为 spark.core.IViewport。

    1.错误描述 TypeError: Error #1034: 强制转换类型失败:无法将 mx.controls::DataGrid@9aa90a1 转换为 spark.core.IViewport. ...

  4. CSS的继承性与优先级

    一.CSS的继承性 在CSS中不可继承的属性:display.margin.padding.border.background.width.min-width.max-width.height.min ...

  5. jquery对象和js对象的转化

    jquery对象和js对象的转化   jquery对象只能使用jqury方法,不能使用js的方法,相反的,js对象也只能使用js的方法,如果js对象使用了jquery方法,那么浏览器就会报错. 但是在 ...

  6. GridView 多余字符显示省略号,并在Tooltip中显示完整信息

    效果 方法一:TemplateField 关键点 TemplateField的灵活性 CSS:overflow:hidden;text-overflow:ellipsis (溢出时隐藏;文本溢出时省略 ...

  7. linux命令类型及执行顺序

    一 为什么要使用命令行   当初级Linux用户面对缺乏图形界面的Linux时很多人都会抱怨:为何要死守命令行?为什么不采用人机互交好.更简单的图形界面呢?事实上,图形界面在某些任务方面确实高效而且简 ...

  8. 爬虫工具fiddle在firefox浏览器中的使用

    1.fiddle工作原理 浏览器与服务器之间通过建立TCP连接以HTTP协议进行通信,默认通过自己发送HTTP(或HTTPS)请求到服务器. 使用Fiddler之后,浏览器像目标服务器发送的请求都会经 ...

  9. 【BZOJ4003】【JLOI2015】城池攻占(左偏树)

    题面 题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi ...

  10. java怎么解析带有特殊字符的字符串

    可以使用StringEscapeUtils这个工具类.