poj 1256 あなぐらむ
2007/09/18
java
poj
へっぽこだけど記録のために公開。
メソッド名とうがとても適当。
はじめ、富豪的にコードかいて(TreeSetとか使って)、なんとなくだめだろうなぁと思いつつ、案の定だめで、
メモリーが足りないがでて、つぎに、時間がたりないがでて、最後に答えがまちがっているがでて、へこんで、
ソートの順序に工夫が必要なことに気がついて、ソートどうしようかなぁと悩んで、解答例をちら見して、見たことに後悔しつつ、Comparatorを使う方向でコードを書いてようやくsubmitされた。
しかし、意味不明な感じのコードになったなぁ。
package p1256;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
for (; 0 < n; n--) {
String line = scanner.nextLine();
char[] cs = line.toCharArray();
Character[] characters = new Character[cs.length];
for (int i = 0; i < characters.length; i++) {
characters[i] = cs[i];
}
Arrays.sort(characters, new Comparator<Character>() {
public int compare(Character o1, Character o2) {
char[] cs = { 'z', 'Z', 'y', 'Y', 'x', 'X', 'w', 'W', 'v',
'V', 'u', 'U', 't', 'T', 's', 'S', 'r', 'R', 'q',
'Q', 'p', 'P', 'o', 'O', 'n', 'N', 'm', 'M', 'l',
'L', 'k', 'K', 'j', 'J', 'i', 'I', 'h', 'H', 'g',
'G', 'f', 'F', 'e', 'E', 'd', 'D', 'c', 'C', 'b',
'B', 'a', 'A', };
char c1 = o1.charValue();
char c2 = o2.charValue();
int p1 = 0;
int p2 = 0;
for (int i = 0; i < cs.length; i++) {
if (cs[i] == c1) {
p1 = i;
}
if (cs[i] == c2) {
p2 = i;
}
}
return p2 - p1;
}
});
for (int i = 0; i < cs.length; i++) {
cs[i] = characters[i];
}
c1(cs);
}
}
static void c1(final char[] cs) {
char pre = (char) -1;
for (int i = 0; i < cs.length; i++) {
char c = cs[i];
if (pre == c) {
continue;
}
pre = c;
char[] ds = ccc(cs, i);
c(new char[] { c }, ds);
}
return;
}
static void c(char[] pc, char[] cs) {
if (cs.length == 0) {
System.out.println(new String(pc));
return;
}
char pre = (char) -1;
for (int i = 0; i < cs.length; i++) {
char c = cs[i];
if (pre == c) {
continue;
}
pre = c;
char[] ds = ccc(cs, i);
c(ddd(pc, c), ds);
}
}
static char[] ccc(char[] cs, int ep) {
char[] cs2 = new char[cs.length - 1];
int j = 0;
for (int i = 0; i < cs.length; i++) {
if (i != ep) {
cs2[j++] = cs[i];
}
}
return cs2;
}
static char[] ddd(char[] p, char c) {
char[] ds = new char[p.length + 1];
System.arraycopy(p, 0, ds, 0, p.length);
ds[ds.length - 1] = c;
return ds;
}
}
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
for (; 0 < n; n--) {
String line = scanner.nextLine();
char[] cs = line.toCharArray();
Character[] characters = new Character[cs.length];
for (int i = 0; i < characters.length; i++) {
characters[i] = cs[i];
}
Arrays.sort(characters, new Comparator<Character>() {
public int compare(Character o1, Character o2) {
char[] cs = { 'z', 'Z', 'y', 'Y', 'x', 'X', 'w', 'W', 'v',
'V', 'u', 'U', 't', 'T', 's', 'S', 'r', 'R', 'q',
'Q', 'p', 'P', 'o', 'O', 'n', 'N', 'm', 'M', 'l',
'L', 'k', 'K', 'j', 'J', 'i', 'I', 'h', 'H', 'g',
'G', 'f', 'F', 'e', 'E', 'd', 'D', 'c', 'C', 'b',
'B', 'a', 'A', };
char c1 = o1.charValue();
char c2 = o2.charValue();
int p1 = 0;
int p2 = 0;
for (int i = 0; i < cs.length; i++) {
if (cs[i] == c1) {
p1 = i;
}
if (cs[i] == c2) {
p2 = i;
}
}
return p2 - p1;
}
});
for (int i = 0; i < cs.length; i++) {
cs[i] = characters[i];
}
c1(cs);
}
}
static void c1(final char[] cs) {
char pre = (char) -1;
for (int i = 0; i < cs.length; i++) {
char c = cs[i];
if (pre == c) {
continue;
}
pre = c;
char[] ds = ccc(cs, i);
c(new char[] { c }, ds);
}
return;
}
static void c(char[] pc, char[] cs) {
if (cs.length == 0) {
System.out.println(new String(pc));
return;
}
char pre = (char) -1;
for (int i = 0; i < cs.length; i++) {
char c = cs[i];
if (pre == c) {
continue;
}
pre = c;
char[] ds = ccc(cs, i);
c(ddd(pc, c), ds);
}
}
static char[] ccc(char[] cs, int ep) {
char[] cs2 = new char[cs.length - 1];
int j = 0;
for (int i = 0; i < cs.length; i++) {
if (i != ep) {
cs2[j++] = cs[i];
}
}
return cs2;
}
static char[] ddd(char[] p, char c) {
char[] ds = new char[p.length + 1];
System.arraycopy(p, 0, ds, 0, p.length);
ds[ds.length - 1] = c;
return ds;
}
}
: