UVa10776 - Determine The Combination(有重复元素的组合问题)

本文介绍了一种用于生成字符串所有可能组合的算法,特别关注于从给定字符串中选取特定数量字符的所有非递减排列方式。该算法适用于离散数学中的排列与组合问题,并提供了一个具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Determine The combination
Input:
standard input
Output: standard output
Time Limit: 1 second


Jahir is a student of NSU (Nice Students' University ). He hates the chapterPermutation& Combination of the subject Discrete Math. But his teacher givehim a assignment to generate all the r combination of a string. But heis too busy with his new girlfriend to do the assignment himself. So hewent to Shabuj, a student ofCSE ( Calculation Science and Engineering ) in BUET (Bangladesh University of Extraordinary Talents ). He asked himto make a program to generate the combinations. But Shabuj is alwayslazy. He wants your help.

Your task is to print alldifferentr combinations of astring s (a rcombination of a string s isacollection of exactly rletters from different positions in s).
There may be different permutations of the same combination; consideronly the one that has its rcharacters in non-decreasing order.
The string consists of only lowercase letters. Any letter can occurmore than once.

 Input

 

The input isconsist of several test cases. Each test case consists of a strings( the length of s is between 1 and 30 ) andan integer r ( 0 < r <= length of s).

Output

 

For each test case you have to print all different r combinations of sin lexicographic order in separate line. You can assume there are atmost 1000 different ones.
 

Sample Input

abcde 2
abcd 3
aba 2

SampleOutput

ab
ac
ad
ae
bc
bd
be
cd
ce
de
abc
abd
acd
bcd
aa
ab

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

const int N = 26;

string s;
int r;
int cnt[N];
int number[N], n;
char ch[N];

bool input();
void solve();
void dfs(int cur, int dep, string s);


int main()
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	while (input()) {
		solve();
	}

	return 0;
}

bool input()
{
	char buf[N + 10];
	
	if (scanf("%s%d", buf, &r) != 2) return false;
	
	s = buf;
	sort(s.begin(), s.end());
	
	memset(cnt, 0x00, sizeof(cnt));
	for (size_t i = 0; i < s.length(); i++) {
		cnt[s[i] - 'a']++; 
	}
	
	n = 0;
	for (int i = 0; i < N; i++) {
		if (cnt[i] != 0) {
			number[n] = cnt[i];
			ch[n++] = i + 'a';
		}
	}
	
	return true;
}

void dfs(int cur, int dep, string s)
{
	if (dep == r) {
		printf("%s\n", s.c_str());;
		return;
	}
	
	for (int i = cur; i < n; i++) {
		if (number[i] > 0) {
			number[i]--;
			string tmp = s;
			tmp.append(1, ch[i]);
			dfs(i, dep + 1, tmp);
			number[i]++;
		}
	}
}

void solve()
{
	dfs(0, 0, "");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值