Category Archives: SRM_323_DIV_2
IrreducibleNumber
nice SRM i solved the 3 problems :D :Dpublic class IrreducibleNumber { public static int getIrreducible(int[] A) { int a = 1; while (true) { if (cansum(A, a)) break; else a++; } return a; } private static boolean cansum(int[] A, int a2) { int sum = 0; for (int i = 0; i < A.length; i++) { if (A[i] == a2) return false; sum += A[i]; } if (a2 == sum) return false; int s1 = A[0]; int s2 = (A.length > 1) ? A[1] : 0; int s3 = (A.length > 2) ? A[2] : 0; if (s1 + s2 == a2) return false; else if (s2 + s3 == a2) return false; else if (s1 + s3 == a2) return false; return true; } }
RoadsAndFools
public class RoadsAndFools { static int score; static String string; public static String determineOrientation(int length, int[] frontSides) { score = 0; int prev = -1; String s = ""; dfs(frontSides[0], prev, frontSides, length, frontSides[0] + "", 1); if (length - frontSides[0] != frontSides[0]) dfs(length - frontSides[0], prev, frontSides, length, (length - frontSides[0]) + "", 1); if (score > 1) return "MULTIPLE SOLUTIONS"; else if (score == 0) return "NO SOLUTION"; else return string; } private static void dfs(int i, int prev, int[] frontSides, int length, String s, int counter) { if (prev >= i) return; else if (counter == frontSides.length) { string = s; score++; } else { prev = i; dfs(frontSides[counter], prev, frontSides, length, s + " " + frontSides[counter], counter + 1); if (length - frontSides[counter] != frontSides[counter]) dfs(length - frontSides[counter], prev, frontSides, length, s + " " + (length - frontSides[counter]), counter + 1); } } public static void main(String[] args) { int[] a = { 5 }; System.out.println(determineOrientation(10, a)); } }
UnrepeatableWords
public class UnrepeatableWords { static String alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static String ans = ""; static boolean check = true; public static String getWord(int k, int n, int allowed) { dfs("A", k, n, allowed, 1); return ans; } private static void dfs(String string, int k, int n, int allowed, int i) { if (i == n&&check(string,k)) { ans = string; check = false; } else if (!check(string, k)) return; else { for (int j = 0; j < allowed && check; j++) { dfs(string + alp.charAt(j), k, n, allowed, i + 1); } } } private static boolean check(String string, int k) { String out = ""; for (int i = 0; i < string.length(); i++) out = string.charAt(i) + out; int n = string.length(); for (int l = 1; l <= n / k; l++) { int count = 0; for (int i = 0; i < k - 1; i++) { if (out.substring(i * l, i * l + l).equals( out.substring((i + 1) * l, (i + 1) * l + l))) { count++; } } if (count == k - 1) return false; } return true; } }