Saturday, April 19, 2014

TCO 2014 Round 1A Div 1 L2 EllysScrabble

// TCO 2014 Round 1A Div 1 L2 EllysScrabble

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class EllysScrabble {
public static void main(String[] args) {
EllysScrabble obj = new EllysScrabble();
System.out.println(
obj.getMin(
"TOPCODER"
, 3
));
}

public String getMin(String letters, int maxDistance) {
char[] c = letters.toCharArray();
char[] dest = new char[c.length];
for (int i = 0; i < dest.length; i++) {
dest[i] = ' ';
}
boolean[] moved = new boolean[c.length];
for (int i = 0; i < c.length; i++) {
char minChar = c[i];
if (moved[i])
minChar = Character.MAX_VALUE;

int idxMinChar = i;
for (int j = maxDistance; j >= -maxDistance; j--) {
if (i + j < 0)
continue;
if (i + j >= c.length)
continue;
if (moved[i + j])
continue;
if (j == -maxDistance && !moved[i + j]) {
idxMinChar = i + j;
break;
}
char cj = c[i + j];
if (cj <= minChar || (cj == minChar && j < 0)) {
minChar = cj;
idxMinChar = i + j;
}
}
if (idxMinChar != i) {
dest[i] = c[idxMinChar];
moved[idxMinChar] = true;
}
else {
dest[i] = c[i];
moved[i] = true;
}
c[i] = c[i];
}

return new String(dest);
}
}

No comments:

Post a Comment