Skip to content

Instantly share code, notes, and snippets.

@PreSoichiSumi
Created April 26, 2016 07:57
Show Gist options
  • Save PreSoichiSumi/e2f2900a1df11b32b6d1514512158ed0 to your computer and use it in GitHub Desktop.
Save PreSoichiSumi/e2f2900a1df11b32b6d1514512158ed0 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main {
private static final double EPS = 1e-10;
static int line = Utils.pow(3, 3);
public static void main(String args[]) {
FastScanner sc = new FastScanner();
int k = sc.nextInt();
int n = sc.nextInt();
int num[]=new int[k];
Arrays.fill(num,0);
String convert[]={
"a","b","c","aa","ab","ac","ba","bb","bc","ca","cb","cc",
"aaa","aab","aac","aba","abb","abc","aca","acb","acc",
"baa","bab","bac","bba","bbb","bbc","bca","bcb","bcc",
"caa","cab","cac","cba","cbb","cbc","cca","ccb","ccc"
} ;
int v[] = new int[n];
String w[] = new String[n];
for(int i=0;i<n;i++){
v[i]=sc.nextInt();
w[i]=sc.next();
}
int range = Utils.pow(4, 3);
range = Utils.pow(range, 3);
for (int i = 0; i < range; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) { //check
StringBuilder tmp=new StringBuilder();
tmp.append(String.valueOf(v[j]));
for(int l=0;l<tmp.length();l++){
char c=tmp.charAt(l);
tmp.deleteCharAt(l);
int convertIndex=num[c-'1'];
tmp.insert(l,convert[convertIndex]);
l+=convert[convertIndex].length()-1;
}
if (!Objects.equals(tmp.toString(), w[j])) {
flag = false;
break;
}
}
if (flag) {
for (int j = 0; j < k; j++) {
System.out.println(convert[num[j]]);
}
break;
}
for(int j=0;j<k;j++){
num[j]++;
if(num[j]<convert.length){
break;
}else{
num[j]=0;
}
}
}
}
}
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