Created
April 26, 2012 15:37
-
-
Save nissuk/2500431 to your computer and use it in GitHub Desktop.
TopCoder SRM 144(550) リファクタ後
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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