这题比较简单,重点应该在如何减少循环次数。
package practice; import java.io.BufferedInputStream; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; /** * DNA sorting * * @author caiyu * @date 2014-11-5 */ public class POJ1007 { public static void main(String[] args) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); int l = cin.nextInt(); int t = cin.nextInt(); String s; Map<Integer, String> m = new TreeMap<Integer, String>(); for (int i = 0; i < t; i++) { s = cin.next(); int[] count = new int[l]; int inversion = 0; for (int j = 0, len = l - 1; j < len; j++) { count[j] = s.charAt(j); for (int h = 0; h < j; h++) { if (count[h] > count[j]) inversion++; } } m.put(inversion, s); } for (String x : m.values()) { System.out.println(x); } } }
时间: 2024-10-04 23:00:34