forked from destiny1020/algorithm_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermTask.java
More file actions
78 lines (65 loc) · 1.78 KB
/
PermTask.java
File metadata and controls
78 lines (65 loc) · 1.78 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package misc;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
/**
* 代表了全排列的一个子任务
*
* @author Destiny
*
*/
public class PermTask implements Callable<Integer> {
private String prefix;
private String remaining;
private int taskThreshold;
private CompletionService<Integer> cs;
private int count;
public PermTask(String prefix, String remaining, int taskThreshold,
CompletionService<Integer> cs) {
this.prefix = prefix;
this.remaining = remaining;
this.taskThreshold = taskThreshold;
this.cs = cs;
}
@Override
public Integer call() throws Exception {
// 如果剩下的字符多于5个,分小任务
if (remaining.length() > taskThreshold) {
for (int i = 0; i < remaining.length(); i++) {
PermTask task = new PermTask(prefix
+ remaining.substring(i, i + 1), remaining.substring(0,
i) + remaining.substring(i + 1), taskThreshold, cs);
cs.submit(task);
PermExecutor.taskCounter.addAndGet(1);
}
return null;
} else {
// 如果剩下的字符少于等于5个,直接执行
perm2(remaining);
return count;
}
}
public void perm2(String str) {
char[] chars = str.toCharArray();
perm2Internal(chars, str.length());
}
private void perm2Internal(char[] chars, int length) {
if (1 == length) {
count++;
// System.out.println(Thread.currentThread().getName() + " : "
// + prefix + Arrays.toString(chars));
return;
}
for (int i = 0; i < length; i++) {
// 让索引为1处的字符和最后一个字符交换位置
exch(chars, i, length - 1);
perm2Internal(chars, length - 1);
// 还原
exch(chars, i, length - 1);
}
}
private void exch(char[] chars, int from, int to) {
char t = chars[from];
chars[from] = chars[to];
chars[to] = t;
}
}