|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | +import java.math.BigInteger; |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + // IO field |
| 10 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static StringTokenizer st; |
| 13 | + |
| 14 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 15 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 16 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 17 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 18 | + |
| 19 | + // Additional field |
| 20 | + static List<String>[] arr; |
| 21 | + static int N; |
| 22 | + static HashMap<String, Integer> hash; |
| 23 | + static List<String> temp; |
| 24 | + |
| 25 | + public static void main(String[] args) throws Exception { |
| 26 | + |
| 27 | + ready(); |
| 28 | + solve(); |
| 29 | + |
| 30 | + bwEnd(); |
| 31 | + } |
| 32 | + |
| 33 | + static void ready() throws Exception{ |
| 34 | + |
| 35 | + N = Integer.parseInt(br.readLine()); |
| 36 | + arr = new List[N]; |
| 37 | + temp = new ArrayList<>(); |
| 38 | + for(int i=0;i<N;i++) { |
| 39 | + arr[i] = new ArrayList<>(); |
| 40 | + String number = ""; |
| 41 | + for(char c:br.readLine().toCharArray()) { |
| 42 | + if(('A'<=c && c<='Z') || ('a'<=c && c<='z')) { |
| 43 | + if(!number.equals("")) { |
| 44 | + arr[i].add(number); |
| 45 | + temp.add(number); |
| 46 | + } |
| 47 | + arr[i].add(""+c); |
| 48 | + number = ""; |
| 49 | + } |
| 50 | + else { |
| 51 | + number += c; |
| 52 | + } |
| 53 | + } |
| 54 | + if(!number.equals("")) { |
| 55 | + arr[i].add(number); |
| 56 | + temp.add(number); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + hash = new HashMap<>(); |
| 62 | + |
| 63 | + Collections.sort(temp, (p,q) -> { |
| 64 | + BigInteger a = new BigInteger(p); |
| 65 | + BigInteger b = new BigInteger(q); |
| 66 | + if(a.compareTo(b) == 1) return 1; |
| 67 | + if(a.compareTo(b) == -1) return -1; |
| 68 | + int a_zeros = 0, b_zeros = 0; |
| 69 | + for(char c:p.toCharArray()) { |
| 70 | + if(c != '0') break; |
| 71 | + a_zeros++; |
| 72 | + } |
| 73 | + for(char c:q.toCharArray()) { |
| 74 | + if(c != '0') break; |
| 75 | + b_zeros++; |
| 76 | + } |
| 77 | + return a_zeros-b_zeros; |
| 78 | + }); |
| 79 | + |
| 80 | + int num = 1; |
| 81 | + String prev = "a"; |
| 82 | + for(String i : temp) { |
| 83 | + if(i == prev) continue; |
| 84 | + hash.put(i, num++); |
| 85 | + prev = i; |
| 86 | + } |
| 87 | + bw.flush(); |
| 88 | + |
| 89 | + for(int i=0;i<26;i++) { |
| 90 | + hash.put(""+(char)(i+65), num++); |
| 91 | + hash.put(""+(char)(i+97), num++); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + static void solve() throws Exception{ |
| 98 | + |
| 99 | + Arrays.sort(arr,(a,b) -> { |
| 100 | + |
| 101 | + for(int i=0;i<Math.min(a.size(), b.size());i++) { |
| 102 | + int valA = hash.get(a.get(i)), valB = hash.get(b.get(i)); |
| 103 | + if(valA-valB < 0) return -1; |
| 104 | + if(valA-valB > 0) return 1; |
| 105 | + } |
| 106 | + return a.size()-b.size(); |
| 107 | + |
| 108 | + }); |
| 109 | + |
| 110 | + for(List<String> L:arr) { |
| 111 | + for(String i:L) bw.write(i); |
| 112 | + bw.write("\n"); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +``` |
0 commit comments