Created
April 26, 2016 07:57
-
-
Save PreSoichiSumi/b5613ec80893d8a1fd0ee739fd8368a7 to your computer and use it in GitHub Desktop.
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
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.*; | |
public class Main { | |
private static final double EPS = 1e-10; | |
public static void main(String args[]) { | |
FastScanner sc = new FastScanner(); | |
int k = sc.nextInt(); | |
int n = sc.nextInt(); | |
int[] width = new int[k]; | |
String[] v = new String[n]; | |
String[] w = new String[n]; | |
for (int i = 0; i < n; i++) { | |
v[i] = sc.next(); | |
w[i] = sc.next(); | |
} | |
int comb = Utils.pow(3, k); | |
for (int i = 0; i < comb; i++) { //幅の全組み合わせについて | |
//幅の組み合わせを生成 | |
StringBuilder widthStr = new StringBuilder(); | |
widthStr.append(Integer.toString(i, 3)); | |
int strlen = widthStr.length(); | |
for (int j = 0; j < (k - strlen); j++) { | |
widthStr.insert(0, '0'); | |
} | |
for (int j = 0; j < k; j++) { | |
width[j] = widthStr.charAt(j) - '0' + 1; | |
} | |
widthStr.setLength(0); | |
//prepare | |
String[] checker = new String[k]; | |
for (int j = 0; j < k; j++) { | |
checker[j] = ""; | |
} | |
//check | |
boolean flag = true; | |
widthcheck: | |
for (int j = 0; j < n; j++) {//各数字と幅1-3の組み合わせ.v[j],w[j]について | |
int index = 0; | |
for (int l = 0; l < v[j].length(); l++) { //1つの入力をチェック | |
int num = v[j].charAt(l) - '1'; //num-1 | |
if ((index + width[num]) > w[j].length()) { //文字幅を超えないか | |
flag = false; | |
break widthcheck; | |
} | |
if (Objects.equals(checker[num], "")) { //矛盾してるか | |
checker[num] = w[j].substring(index, index + width[num]); | |
} else if (!Objects.equals(checker[num], w[j].substring(index, index + width[num]))) { | |
flag = false; | |
break widthcheck; | |
} | |
index += width[num]; //次の文字へ | |
} | |
if (index != (w[j].length())) { | |
flag = false; | |
break; | |
} | |
} | |
if (flag) { | |
for (String str : checker) { | |
System.out.println(str); | |
} | |
break; | |
} | |
} | |
} | |
} | |
class Utils { | |
public static int pow(int b, int k) throws IllegalArgumentException { | |
if (k < 0) | |
throw new IllegalArgumentException(); | |
switch (b) { | |
case 0: | |
return (k == 0) ? 1 : 0; | |
case 1: | |
return 1; | |
case (-1): | |
return ((k & 1) == 0) ? 1 : -1; | |
case 2: | |
return (k < Integer.SIZE) ? (1 << k) : 0; | |
case (-2): | |
if (k < Integer.SIZE) { | |
return ((k & 1) == 0) ? (1 << k) : -(1 << k); | |
} else { | |
return 0; | |
} | |
default: | |
} | |
for (int accum = 1; ; k >>= 1) { | |
switch (k) { | |
case 0: | |
return accum; | |
case 1: | |
return b * accum; | |
default: | |
accum *= ((k & 1) == 0) ? 1 : b; | |
b *= b; | |
} | |
} | |
} | |
} | |
class FastScanner { | |
private final InputStream in = System.in; | |
private final byte[] buffer = new byte[1024]; | |
private int ptr = 0; | |
private int buflen = 0; | |
private boolean hasNextByte() { | |
if (ptr < buflen) { | |
return true; | |
} else { | |
ptr = 0; | |
try { | |
buflen = in.read(buffer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
if (buflen <= 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private int readByte() { | |
if (hasNextByte()) return buffer[ptr++]; | |
else return -1; | |
} | |
private boolean isPrintableChar(int c) { | |
return 33 <= c && c <= 126; | |
} | |
private void skipUnprintable() { | |
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; | |
} | |
public boolean hasNext() { | |
skipUnprintable(); | |
return hasNextByte(); | |
} | |
public String next() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
StringBuilder sb = new StringBuilder(); | |
int b = readByte(); | |
while (isPrintableChar(b)) { | |
sb.appendCodePoint(b); | |
b = readByte(); | |
} | |
return sb.toString(); | |
} | |
public long nextLong() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
long n = 0; | |
boolean minus = false; | |
int b = readByte(); | |
if (b == '-') { | |
minus = true; | |
b = readByte(); | |
} | |
if (b < '0' || '9' < b) { | |
throw new NumberFormatException(); | |
} | |
while (true) { | |
if ('0' <= b && b <= '9') { | |
n *= 10; | |
n += b - '0'; | |
} else if (b == -1 || !isPrintableChar(b)) { | |
return minus ? -n : n; | |
} else { | |
throw new NumberFormatException(); | |
} | |
b = readByte(); | |
} | |
} | |
public int nextInt() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
int n = 0; | |
boolean minus = false; | |
int b = readByte(); | |
if (b == '-') { | |
minus = true; | |
b = readByte(); | |
} | |
if (b < '0' || '9' < b) { | |
throw new NumberFormatException(); | |
} | |
while (true) { | |
if ('0' <= b && b <= '9') { | |
n *= 10; | |
n += b - '0'; | |
} else if (b == -1 || !isPrintableChar(b)) { | |
return minus ? -n : n; | |
} else { | |
throw new NumberFormatException(); | |
} | |
b = readByte(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment