import java.util.Arrays; import java.util.List; import java.util.Scanner; public class slo { private static final String A = "a"; private static final String B = "b"; private static final String C = "c"; private static final String NIE = "NIE"; private static final List<String> LETTERS = Arrays.asList(A, B, C); private static int count = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int max_len = sc.nextInt(); int elem = sc.nextInt(); StringBuilder word = new StringBuilder(); boolean found = false; for (String letter : LETTERS) { if (!found) { found = searchTree(1, max_len, elem, word, letter); } } if (!found) { System.out.println(NIE); } } private static boolean searchTree(int depth, int max_len, int elem, StringBuilder myWord, String letter) { myWord.append(letter); count++; if (elem == count) { System.out.println(myWord.toString()); return true; } if (depth < max_len) { if (searchTree(depth + 1, max_len, elem, myWord, getLeft(letter))) { return true; } if (searchTree(depth + 1, max_len, elem, myWord, getRight(letter))) { return true; } } myWord.setLength(myWord.length() - 1); return false; } private static String getLeft(String letter) { switch (letter) { case A: return B; case B: return A; case C: return A; default: return "0"; } } private static String getRight(String letter) { switch (letter) { case A: return C; case B: return C; case C: return B; default: return "0"; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import java.util.Arrays; import java.util.List; import java.util.Scanner; public class slo { private static final String A = "a"; private static final String B = "b"; private static final String C = "c"; private static final String NIE = "NIE"; private static final List<String> LETTERS = Arrays.asList(A, B, C); private static int count = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int max_len = sc.nextInt(); int elem = sc.nextInt(); StringBuilder word = new StringBuilder(); boolean found = false; for (String letter : LETTERS) { if (!found) { found = searchTree(1, max_len, elem, word, letter); } } if (!found) { System.out.println(NIE); } } private static boolean searchTree(int depth, int max_len, int elem, StringBuilder myWord, String letter) { myWord.append(letter); count++; if (elem == count) { System.out.println(myWord.toString()); return true; } if (depth < max_len) { if (searchTree(depth + 1, max_len, elem, myWord, getLeft(letter))) { return true; } if (searchTree(depth + 1, max_len, elem, myWord, getRight(letter))) { return true; } } myWord.setLength(myWord.length() - 1); return false; } private static String getLeft(String letter) { switch (letter) { case A: return B; case B: return A; case C: return A; default: return "0"; } } private static String getRight(String letter) { switch (letter) { case A: return C; case B: return C; case C: return B; default: return "0"; } } } |