poj 1007
2007/09/17
java
poj
1007 -- DNA Sorting
バブルソート
ソートのカウントはバブルソートで。
このサブミットでようやく37個終了。
簡単なもの(自分で解けるか、解答例があるもの)しか解いてないので、だめだめなんだけど。
package p1007;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int len = scanner.nextInt();
int n = scanner.nextInt();
List<DNAString> list = new ArrayList<DNAString>();
scanner.nextLine();
for (; 0 < n; n--) {
String line = scanner.nextLine();
list.add(new DNAString(line));
}
Collections.sort(list);
Iterator<DNAString> iterable = list.iterator();
while (iterable.hasNext()) {
System.out.println(iterable.next());
}
}
static class DNAString implements Comparable<Object> {
Integer d;
String s;
public DNAString(String s) {
this.s = s;
d = sort_count(s.toCharArray());
}
public int compareTo(Object o) {
return d.compareTo(((DNAString) o).d);
}
public String toString() {
return this.s;
}
}
static int sort_count(char[] cs) {
int cnt = 0;
for (int i = 0; i < cs.length - 1; i++) {
for (int j = cs.length - 1; j > i; j--) {
if (cs[j] < cs[j - 1]) {
char t = cs[j];
cs[j] = cs[j - 1];
cs[j - 1] = t;
cnt++;
}
}
}
return cnt;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int len = scanner.nextInt();
int n = scanner.nextInt();
List<DNAString> list = new ArrayList<DNAString>();
scanner.nextLine();
for (; 0 < n; n--) {
String line = scanner.nextLine();
list.add(new DNAString(line));
}
Collections.sort(list);
Iterator<DNAString> iterable = list.iterator();
while (iterable.hasNext()) {
System.out.println(iterable.next());
}
}
static class DNAString implements Comparable<Object> {
Integer d;
String s;
public DNAString(String s) {
this.s = s;
d = sort_count(s.toCharArray());
}
public int compareTo(Object o) {
return d.compareTo(((DNAString) o).d);
}
public String toString() {
return this.s;
}
}
static int sort_count(char[] cs) {
int cnt = 0;
for (int i = 0; i < cs.length - 1; i++) {
for (int j = cs.length - 1; j > i; j--) {
if (cs[j] < cs[j - 1]) {
char t = cs[j];
cs[j] = cs[j - 1];
cs[j - 1] = t;
cnt++;
}
}
}
return cnt;
}
}
: