1 /*
2 * Created on Oct 22, 2004
3 *
4 * To change the template for this generated file go to
5 * Window>Preferences>Java>Code Generation>Code and Comments
6 */
7
8 /**
9 * @author WangBNU helloquota@163.com My Homepage http://bnulinux.yeah.net To
10 * change the template for this generated type comment go to
11 * Window>Preferences>Java>Code Generation>Code and Comments
12 */
13 public class PaiLie {
14 public static void main(String args[]) {
15 int leng = args[0].length();
16 int[] a = new int[leng];
17 boolean[] c = new boolean[leng];
18 for (int i = 0; i < leng; i++) {
19 a[i] = i + 1;
20 c[i] = false;
21 }
22 int ii = findActiveM(a, c);
23
24 for (int i = 0; i < args[0].length(); i++)
25 System.out.print(a[i]);
26 System.out.println();
27
28 if (ii >= 0) {
29 do {
30 int activeM = a[ii];
31 if (c[ii] == false) {
32 swapInt(a, ii, ii - 1);
33 swapBoolean(c, ii, ii - 1);
34 } else {
35 swapInt(a, ii, ii + 1);
36 swapBoolean(c, ii, ii + 1);
37 }
38 for (int i = 0; i < args[0].length(); i++) {
39 System.out.print(a[i]);
40 if (a[i] > activeM)
41 c[i] = !c[i];
42 }
43 System.out.println();
44 ii = findActiveM(a, c);
45
46 } while (ii >= 0);
47 }
48 }
49
50 /**
51 * @param aa
52 *
53 * @param a
54 * @param b
55 */
56 private static void swapInt(int[] aa, int a, int b) {
57 int temp = aa[a];
58 aa[a] = aa[b];
59 aa[b] = temp;
60 }
61
62 private static void swapBoolean(boolean[] aa, int a, int b) {
63 boolean temp = aa[a];
64 aa[a] = aa[b];
65 aa[b] = temp;
66 }
67
68 private static int findActiveM(int[] a, boolean[] c) {
69
70 int first1 = 0, first2 = 0;
71 int m = -1, n = -1;
72 for (int i = 1; i < a.length - 1; i++) {
73 if (c[i] == false) {
74 if ((a[i] > a[i - 1]) && a[i] > first1) {
75 first1 = a[i];
76 m = i;
77 }
78 } else {
79 if ((a[i] > a[i + 1]) && a[i] > first2) {
80 first2 = a[i];
81 n = i;
82 }
83 }
84 }
85 if ((c[0] == true) && (a[0] > a[1]) && (a[0] > first2)) {
86 first2 = a[0];
87 n = 0;
88 }
89 if ((c[a.length - 1] == false) && (a.length > 1)
90 && (a[a.length - 1] > a[a.length - 2])
91 && (a[a.length - 1] > first1)) {
92 first1 = a[a.length - 1];
93 m = a.length - 1;
94 }
95 if (first1 >= first2)
96 return m;
97 else
98 return n;
99 }
100
101 }
102