Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static int getDoublePosStart(StringBuilder sb, int start, int startlen) {
- for (; start < startlen; ++start) {
- if (sb.charAt(start) == sb.charAt(start - 1)) {
- return start;
- }
- }
- return -1;
- }
- public static int getDoublePosEnd(StringBuilder sb, int limit) {
- // for (int i = sb.length() - 1; i > limit; --i) {
- int i = sb.length() - 1;
- if (i > limit) {
- if (sb.charAt(i) == sb.charAt(i - 1)) {
- return i;
- }
- }
- return -1;
- }
- public static int getNonDoublePos(StringBuilder sb, char c, int oldpos) {
- for (int i = oldpos; i > 0; --i) {
- if (sb.charAt(i) != c && sb.charAt(i - 1) != c) {
- return i;
- }
- }
- return 0;
- }
- public static String xjarjestaMerkit(String mjono) {
- int amin = 255;
- int amax = 0;
- int[] amounts = new int[256];
- for (int i = 0; i < mjono.length(); ++i) {
- char c = mjono.charAt(i);
- ++amounts[c];
- amin = Math.min(amin, c);
- amax = Math.max(amax, c);
- }
- int[] firstcharloc = new int[256];
- boolean first = true;
- StringBuilder result = new StringBuilder();
- int lastpos = 1;
- for (char i = (char)amin; i <= amax; ++i) {
- if (amounts[i] <= 0) {
- continue;
- }
- // optimizaion for first
- if (first) {
- for (int j = 0; j < amounts[i]; ++j) {
- result.append(i);
- }
- first = false;
- continue;
- }
- int startlen = result.length();
- for (int j = 0; j < amounts[i]; ++j) {
- int samepos = getDoublePosStart(result, lastpos, startlen);
- if (samepos >= 0) {
- result.insert(samepos, i);
- lastpos = samepos + 1;
- ++startlen;
- } else {
- firstcharloc[i]++;
- result.append(i);
- if (i == amax) {
- int strend = result.length()-firstcharloc[i];
- ++j;
- for (; j < amounts[i]; ++j) {
- int pos2 = getNonDoublePos(result, i, strend);
- result.insert(pos2, i);
- lastpos++;
- strend = pos2;
- }
- break;
- }
- }
- }
- }
- char oldchar = result.charAt(result.length() - 1);
- int oldpos = result.length() - firstcharloc[oldchar];
- while (true) {
- int pos = getDoublePosEnd(result, lastpos);
- if (pos < 0) {
- break;
- } else {
- char c = result.charAt(pos);
- if (c != oldchar) {
- oldpos = result.length() - firstcharloc[c];
- }
- int pos2 = getNonDoublePos(result, c, oldpos);
- result.deleteCharAt(pos);
- result.insert(pos2, c);
- lastpos++;
- }
- }
- return result.toString();
- }
- static int amin = 255;
- static int amax = 0;
- static int total = 0;
- static int[] amounts = new int[256];
- public static char getNextChar(char lastChar)
- {
- char next = 255;
- for (char i = (char)amin; i <= amax && total > 0; ++i) {
- if (amounts[i] <= 0) {
- continue;
- }
- if (i == lastChar) {
- continue;
- }
- if (amounts[i] > total/2) {
- next = i;
- break;
- }
- next = (char)Math.min(next, i);
- }
- --amounts[next];
- --total;
- return next;
- }
- public static String jarjestaMerkit(String mjono) {
- for (int i = 0; i < mjono.length(); ++i) {
- char c = mjono.charAt(i);
- ++amounts[c];
- ++total;
- amin = Math.min(amin, c);
- amax = Math.max(amax, c);
- }
- StringBuilder sb = new StringBuilder();
- char c = 0;
- while (total > 0)
- {
- c = getNextChar(c);
- sb.append(c);
- }
- return sb.toString();
- }
- public static void main(String[] args) {
- if (true) {
- suuri3();
- return;
- }
- System.out.println(jarjestaMerkit("AAABBB"));
- System.out.println(jarjestaMerkit("AAABBBCCCCCCCDDDDD"));
- System.out.println(jarjestaMerkit("BAAACCC"));
- System.out.println(jarjestaMerkit("CBAED"));
- System.out.println(jarjestaMerkit("AXAXA"));
- }
- public static void suuriTesti(String alku, String loppu) {
- String uusi = jarjestaMerkit(alku);
- System.out.println(uusi.equals(loppu));
- System.out.println(uusi);
- System.out.println(loppu);
- System.out.println(uusi.length());
- System.out.println(uusi.substring(uusi.length() - 200));
- }
- public static void suuri3() {
- int n = 99999;
- char[] t = new char[n];
- char[] u = new char[n];
- for (int i = 0; i < n; i++) {
- if (i % 2 == 0) {
- t[i] = 'X';
- } else if (i < n / 2) {
- t[i] = 'A';
- } else {
- t[i] = 'B';
- }
- u[i] = t[i];
- }
- Arrays.sort(u);
- suuriTesti(new String(u), new String(t));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement