Created
February 19, 2013 17:24
-
-
Save torazuka/4987962 to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書く第8回の参考問題(Java解答)
This file contains 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
import static org.junit.Assert.assertEquals; | |
import java.util.EnumSet; | |
import org.junit.Test; | |
enum Token { | |
c("0101101"), l("010111"), d("01010"), s("0010"), n("0011"), o("0110"), i( | |
"0100"), a("0111"), r("1100"), h("1101"), t("000"), end("111"), e( | |
"10"); | |
private String str; | |
private Token(String s) { | |
str = s; | |
} | |
String getStr() { | |
return str; | |
} | |
} | |
/** | |
* 問題: http://nabetani.sakura.ne.jp/hena/ord8entco/ | |
*/ | |
public class Entco { | |
private int use; | |
protected String match(String str, int cur) { | |
StringBuilder result = new StringBuilder(); | |
EnumSet<Token> allOf = EnumSet.allOf(Token.class); | |
Token tmp = null; | |
for (Token each : allOf) { | |
if (cur + each.getStr().length() < str.length() + 1) { | |
if (each.getStr().equals( | |
str.substring(cur, cur + each.getStr().length()))) { | |
tmp = each; | |
use += tmp.getStr().length(); | |
if (each.name().equals("end")) { | |
return new String(result); | |
} else { | |
result.append(each.name()); | |
} | |
break; | |
} | |
} | |
} | |
if (tmp == null) { | |
throw new IllegalArgumentException(); | |
} | |
if (cur < str.length() && tmp.name().equals("end") == false) { | |
result.append(match(str, cur + tmp.getStr().length())); | |
} | |
return new String(result); | |
} | |
protected String solve(String input) { | |
char[] charArray = input.toCharArray(); | |
StringBuilder sb = new StringBuilder(); | |
for (char c : charArray) { | |
byte bt = Byte.parseByte(String.valueOf(c), 16); | |
String str = Integer.toString(bt, 2); | |
StringBuilder tmp = new StringBuilder(); | |
for (int i = 1; i < str.length() + 1; i++) { | |
tmp.append(str.charAt(str.length() - i)); | |
} | |
if (tmp.length() < 4) { | |
for (int i = tmp.length(); i < 4; i++) { | |
tmp.append('0'); | |
} | |
} | |
sb.append(tmp); | |
} | |
StringBuilder result = new StringBuilder(); | |
try { | |
result.append(match(new String(sb), 0)); | |
} catch (IllegalArgumentException e) { | |
return "*invalid*"; | |
} | |
result.append(":"); | |
result.append(use); | |
return new String(result); | |
} | |
protected void test(String input, String expected) { | |
Entco target = new Entco(); | |
assertEquals(expected, target.solve(input)); | |
} | |
@Test | |
public void testEntco() throws Exception { | |
/* 0 */test("16d9d4fbd", "ethanol:30"); | |
/* 1 */test("df", "e:5"); | |
/* 2 */test("ad7", "c:10"); | |
/* 3 */test("870dcb", "t:6"); | |
/* 4 */test("880f63d", "test:15"); | |
/* 5 */test("a57cbe56", "cat:17"); | |
/* 6 */test("36abef2", "roll:23"); | |
/* 7 */test("ad576cd8", "chant:25"); | |
/* 8 */test("3e2a3db4fb9", "rails:25"); | |
/* 9 */test("51aa3b4c2", "eeeteee:18"); | |
/* 10 */test("ad5f1a07affe", "charset:31"); | |
/* 11 */test("4ab8a86d7afb0f", "slideshare:42"); | |
/* 12 */test("ac4b0b9faef", "doctor:30"); | |
/* 13 */test("cafebabe", "nlh:17"); | |
/* 14 */test("43e7", "sra:15"); | |
/* 15 */test("53e7", "eera:15"); | |
/* 16 */test("86cf", "tera:16"); | |
/* 17 */test("b6cf", "hon:15"); | |
/* 18 */test("0", "*invalid*"); | |
/* 19 */test("c", "*invalid*"); | |
/* 20 */test("d", "*invalid*"); | |
/* 21 */test("e", "*invalid*"); | |
/* 22 */test("babecafe", "*invalid*"); | |
/* 23 */test("8d", "*invalid*"); | |
/* 24 */test("ad", "*invalid*"); | |
/* 25 */test("af", "*invalid*"); | |
/* 26 */test("ab6e0", "*invalid*"); | |
/* 27 */test("a4371", "*invalid*"); | |
/* 28 */test("a4371", "*invalid*"); | |
/* 29 */test("96e3", "*invalid*"); | |
/* 30 */test("0dc71", "*invalid*"); | |
/* 31 */test("2a9f51", "*invalid*"); | |
/* 32 */test("a43fb2", "*invalid*"); | |
/* 33 */test("ab6e75", "*invalid*"); | |
/* 34 */test("a5dcfa", "*invalid*"); | |
/* 35 */test("ca97", "*invalid*"); | |
/* 36 */test("6822dcb", "*invalid*"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment