Created
February 3, 2013 19:04
-
-
Save suicide/4703182 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2013 Round 1 Security Backtracking java solution too slow
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 java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Scanner; | |
/** | |
* @author psy | |
* | |
*/ | |
public class Security { | |
private static final String IMPOSSIBLE = "IMPOSSIBLE"; | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
List<String> output = new LinkedList<String>(); | |
Security testCase = new Security(); | |
int games = Integer.parseInt(in.nextLine()); | |
for (int i = 1; i <= games; i++) { | |
String m = in.nextLine(); | |
String k1 = in.nextLine(); | |
String k2 = in.nextLine(); | |
String result = testCase.findKey(Integer.parseInt(m), k1, k2); | |
output.add("Case #" + i + ": " + result); | |
} | |
for (String out : output) { | |
System.out.println(out); | |
} | |
} | |
public String findKey(int m, String k1, String k2) { | |
int l = k1.length() / m; | |
// split sections | |
String regex = "(?<=\\G.{" + l + "})"; | |
List<String> k1Sections = Arrays.asList(k1.split(regex)); | |
List<String> k2Sections = Arrays.asList(k2.split(regex)); | |
return findKey(k1Sections, k2Sections); | |
} | |
private String findKey(List<String> k1Sections, List<String> k2Sections) { | |
if (k1Sections.size() == 1) { | |
if (matchSection(k1Sections.get(0), k2Sections.get(0))) { | |
return lexSmall(k1Sections.get(0), k2Sections.get(0)); | |
} else { | |
return IMPOSSIBLE; | |
} | |
} | |
String s1 = k1Sections.get(0); | |
String result = IMPOSSIBLE; | |
for (String s2 : k2Sections) { | |
if (matchSection(s1, s2)) { | |
List<String> newK2Sections = new ArrayList<String>(k2Sections); | |
newK2Sections.remove(s2); | |
result = findKey(k1Sections.subList(1, k1Sections.size()), newK2Sections); | |
} | |
if (result != IMPOSSIBLE) { | |
result = lexSmall(s1, s2) + result; | |
break; | |
} | |
} | |
return result; | |
} | |
private String lexSmall(String s1, String s2) { | |
int length = s1.length(); | |
String result = ""; | |
for (int i = 0; i < length; i++) { | |
char c1 = s1.charAt(i); | |
char c2 = s2.charAt(i); | |
if (c1 == '?') { | |
if (c2 == '?') { | |
result += 'a'; | |
} else { | |
result += c2; | |
} | |
} else { | |
result += c1; | |
} | |
} | |
return result; | |
} | |
private boolean matchSection(String s1, String s2) { | |
if (s1.equals(s2)) { | |
return true; | |
} | |
// iterate | |
int length = s1.length(); | |
for (int i = 0; i < length; i++) { | |
if (s1.charAt(i) != '?' && s2.charAt(i) != '?' && s1.charAt(i) != s2.charAt(i)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment