poj1256

简单题

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

char st[20];
int num[20];
int n;
int len;
char ans[20];

bool cmp(const char &a, const char &b)
{
int x = a - 'A';
int y = b - 'A';
if (x >= 26)
x
-= 'a' - 'A';
if (y >= 26)
y
-= 'a' - 'A';
if (x != y)
return x < y;
return a < b;
}

void dfs(int a)
{
if (a == len)
{
ans[a]
= '\0';
printf(
"%s\n", ans);
return;
}
for (int i = 0; i < n; i++)
if (num[i] > 0)
{
ans[a]
= st[i];
num[i]
--;
dfs(a
+ 1);
num[i]
++;
}
}

int main()
{
// freopen("t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
scanf(
"%s", st);
sort(st, st
+ strlen(st), cmp);
memset(num,
0, sizeof(num));
int j = 0;
len
= strlen(st);
for (int i = 0; i < len; i++)
if (st[i] == st[j])
num[j]
++;
else
{
st[
++j] = st[i];
num[j]
= 1;
}
n
= j + 1;
dfs(
0);
}
return 0;
}

原文地址:https://www.cnblogs.com/rainydays/p/2089132.html