UVa 10098 - Generating Fast

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

先 sort 原字串後,做 backtracking 即可。

Code

UVa 10098UVa 10098 - Generating Fast
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 12
using namespace std;

bool used[N];
int len;
char now[N], str[N];
void backtracking(int n);
int main()
{
int Case;
scanf("%d", &Case);
getchar();
while (Case--)
{
gets(str);
len = strlen(str);
fill(used, used + len, false);
sort(str, str + len);

backtracking(0);
putchar('\n');
}

return 0;
}
void backtracking(int n)
{
int i;
if (n == len)
{
now[n] = '\0';
puts(now);
}
else
{
char last = '\0';
for (i = 0; i < len; i++)
{
if (!used[i] && str[i] != last)
{
used[i] = true;
now[n] = str[i];
last = str[i];

backtracking(n + 1);

used[i] = false;
}
}
}
}