Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created April 26, 2012 15:37
Show Gist options
  • Save nissuk/2500431 to your computer and use it in GitHub Desktop.
Save nissuk/2500431 to your computer and use it in GitHub Desktop.
TopCoder SRM 144(550) リファクタ後
public class BinaryCode {
public String[] decode(String message) {
return new String[] { assume('0', message), assume('1', message) };
}
protected String assume(char p0, String message) {
char[] q = message.toCharArray();
int len = q.length;
char[] p = new char[len];
p[0] = p0;
int i, pSum;
for (i = 0; i < len - 1; i++) {
pSum = (0 < i ? num(p[i-1]) : 0) + num(p[i]);
p[i+1] = (char)('0' + num(q[i]) - pSum);
if ('2' <= p[i+1]) return "NONE";
}
pSum = (0 < i ? num(p[i-1]) : 0) + num(p[i]);
return (num(q[i]) == pSum) ? new String(p) : "NONE";
}
protected int num(char c) { return c - '0'; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment