习题 80:全排列★★
2008-10-02 12:29
113 查看
/*
题目描述:
有重复元素的全排列问题
输入:
有多组测试数据,每组只有一行不超过100个且不包含空格的字符组成的串
当遇到EOF标志时结束程序。
输出:
对于每组测试,输出由那组字符所能排列出来的所有组合,
按Ascii码值比较,按字典顺序输出
样例输入:
123
baca
样例输出:
123
132
213
231
312
321
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa
*/
//给出一个字符串的所有排列,重复项过滤掉
#include "StdAfx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int len;
int flags[256];
//char max,min;
//destination, 排序后的字符串
//insrPos,字母插入位置
//len, 待排序字符串总长度
void allRank(char* destination,int insrPos,char max, char min)
{
// static int count;
int i = 0;
//recursion finished
if(insrPos == len)
{
//printf("%d:%s,len = %d, insrPos = %d/n",++count,destination, len, insrPos);
//printf("%s/n",destination);
puts(destination);
return ;
}
i = min;
while(i <= max)
{
//字母exists
if( flags[i])
{
destination[insrPos] = i;
flags[i] -= 1;
allRank(destination,insrPos+1,max,min);
flags[i] += 1;
}
i++;
}
}
void allArray(char* source)
{
int i;
char max, min;
//char *p = (char*) malloc(sizeof(char)*(len + 1));
char p[101] = {0};
len = strlen(source);
for(i = 0; i < 256; i++)
{
flags[i] = 0;
}
max = min = flags[0];
//record
for(i = 0; i < len; i++)
{
flags[source[i]] +=1;
if(max < source[i])
max = source[i];
else if(min > source[i])
min = source[i];
}
p[len] = '/0';
allRank(p,0,max,min);
//free(p);
}
int main(int argc, char *argv[])
{
char c[101];
//while(EOF!=scanf("%s",c))
while(cin >> c)
{
allArray(c);
//getchar();
}
return 0;
}
/*
79380
Name: "younthu" Problem ID "80"
Submit Time: 2008/10/02-10:55
G++: Compile Warning:
Line In function `void allArray(char*)':
Line 56: warning: array subscript has type `char'
Test 1: Accepted Time = 20 ms
--------------------------------
Problem ID 80
Test Result Accepted
Total Time 20 ms
Total Memory 160 Kb / 1000 Kb
Code Length 980 Bytes
*/
这个方法不是最好的,还有耗时为0(同样的测试case)的方法。用的是stl里面的算法。
copy别人的源码如下:
题目描述:
有重复元素的全排列问题
输入:
有多组测试数据,每组只有一行不超过100个且不包含空格的字符组成的串
当遇到EOF标志时结束程序。
输出:
对于每组测试,输出由那组字符所能排列出来的所有组合,
按Ascii码值比较,按字典顺序输出
样例输入:
123
baca
样例输出:
123
132
213
231
312
321
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa
*/
//给出一个字符串的所有排列,重复项过滤掉
#include "StdAfx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int len;
int flags[256];
//char max,min;
//destination, 排序后的字符串
//insrPos,字母插入位置
//len, 待排序字符串总长度
void allRank(char* destination,int insrPos,char max, char min)
{
// static int count;
int i = 0;
//recursion finished
if(insrPos == len)
{
//printf("%d:%s,len = %d, insrPos = %d/n",++count,destination, len, insrPos);
//printf("%s/n",destination);
puts(destination);
return ;
}
i = min;
while(i <= max)
{
//字母exists
if( flags[i])
{
destination[insrPos] = i;
flags[i] -= 1;
allRank(destination,insrPos+1,max,min);
flags[i] += 1;
}
i++;
}
}
void allArray(char* source)
{
int i;
char max, min;
//char *p = (char*) malloc(sizeof(char)*(len + 1));
char p[101] = {0};
len = strlen(source);
for(i = 0; i < 256; i++)
{
flags[i] = 0;
}
max = min = flags[0];
//record
for(i = 0; i < len; i++)
{
flags[source[i]] +=1;
if(max < source[i])
max = source[i];
else if(min > source[i])
min = source[i];
}
p[len] = '/0';
allRank(p,0,max,min);
//free(p);
}
int main(int argc, char *argv[])
{
char c[101];
//while(EOF!=scanf("%s",c))
while(cin >> c)
{
allArray(c);
//getchar();
}
return 0;
}
/*
79380
Name: "younthu" Problem ID "80"
Submit Time: 2008/10/02-10:55
G++: Compile Warning:
Line In function `void allArray(char*)':
Line 56: warning: array subscript has type `char'
Test 1: Accepted Time = 20 ms
--------------------------------
Problem ID 80
Test Result Accepted
Total Time 20 ms
Total Memory 160 Kb / 1000 Kb
Code Length 980 Bytes
*/
这个方法不是最好的,还有耗时为0(同样的测试case)的方法。用的是stl里面的算法。
copy别人的源码如下:
#include <iostream> #include <algorithm> using namespace std; char s[110]; int main() { while( scanf("%s",s)!=EOF ) { int len = strlen(s); sort(s,s+len); do { printf("%s/n",s); } while(next_permutation(s,s+len)); } return 0; }
/*
Name: "lantionzy" Problem ID "80" Submit Time: 2008/9/21-14:01 G++: Compile OK Test 1: Accepted Time = 0 ms -------------------------------- Problem ID 80 Test Result Accepted Total Time 0 ms Total Memory 160 Kb / 1000 Kb Code Length 268 Bytes */
相关文章推荐
- 操作系统习题1-80【来自无忧】
- 习题2.8 输出全排列
- 习题 4.8 给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。90分以上为‘A’,80~89分为‘B’,70~79分为‘C’,60~69分为‘D’,60分以下为’E‘。
- 算法习题53:字符串全排列问题
- Algorithms 第四版 习题 4.1.16- 4.1.18
- 章三习题4——UVA 455
- 1到n的全排列
- 章三习题8——UVA 202 Repearing Decimals
- 陶哲轩实分析 习题 7.1.5
- 80. Remove Duplicates from Sorted Array II
- C++ Primer习题5.18
- 紫书章三习题5——UVA 227 Puzzle
- C++primer习题6.20
- C++Primer第五版 第十章习题答案(41~42)
- 《算法导论》CH6 习题6.3-3的证明
- 【LeetCode31-40】全排列,栈的用法,find用法,数独规则及解法,数列,类似找钱问题找出所有可能,不允许重复的找钱问题
- 线性代数与矩阵论 习题 1.2.2
- 回溯最简单的全排列
- IIS与wamp的Apache端口冲突了,冲突端口为80
- 【UOJ 80】 二分图最大权匹配