POJ 1256 Anagram
来源:互联网 发布:软件开发的发展前景 编辑:程序博客网 时间:2024/05/29 04:46
Description
Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba".
In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.
Input
Output
Sample Input
3aAbabcacba
Sample Output
AabAbaaAbabAbAabaAabcacbbacbcacabcbaaabcaacbabacabcaacabacbabaacbacabcaacaabcabacbaa
Hint
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
Source
回溯法
#include<stdio.h>
#include<algorithm>
#include<cctype>
using namespace std;
char s[20];
char res[20];
bool b[20];
int N;
int cmp(char a,char b)
{
if(islower(a)&&islower(b)) return a<b;
if(isupper(a)&&isupper(b)) return a<b;
if(islower(a)&&isupper(b)) return a<(b-'A'+'a');
if(isupper(a)&&islower(b)) return a<=(b-'a'+'A');
}
void backtrace(int n)
{
if(n==N)
{
printf("%s/n",res);
return ;
}
int i;
for(i=0; i<N; ++i)
{
if(b[i]==false)
{
res[n]=s[i];
b[i]=true;
backtrace(n+1);
b[i]=false;
while(i<(N-1) && s[i]==s[i+1])//跳过相同的字符
i++;
}
}
}
int main()
{
int T;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
scanf("%s",&s);
N=strlen(s);
res[N]='/0';
sort(s,s+N,cmp);
memset(b,0,sizeof(b));
backtrace(0);
}
}
return 0;
}
- POJ 1256 Anagram
- POJ 1256 Anagram
- poj 1256 Anagram
- poj 1256 Anagram
- POJ 1256 Anagram
- poj 1256 Anagram
- poj 1256 Anagram
- poj 1256 Anagram
- Anagram poj 1256
- poj 1256(Anagram)
- POJ - 1256 Anagram
- 7 POJ 1256 Anagram
- poj 1256 Anagram
- POJ 1256:Anagram
- POJ 1256 Anagram
- POJ - 1256 Anagram
- poj 1256 Anagram
- poj-1256-Anagram
- CB5:Error E2266: No file names given
- POJ 2352 Stars
- xp 中支持php的环境的iis配置
- sql*plus命令 之 文件操作命令
- 参考SlidingDrawer的Android animation 的算法
- POJ 1256 Anagram
- VC如何打开一个固定大小的视图
- 如何使用范型技术在C++中添加对JavaScript的支持
- asp.net的总结(转帖)
- 理解Service
- 一次URL请求后台响应两次
- qq临时对话代码,MSN、淘宝旺旺、Skype在线对话代码
- 做一下mssql的连接,和赋值的测试
- 曾经一份资源在我面前,我没有好好珍惜(原来是没有积分)