Did you add something to input of this program? I got the judge data and my Java solution works just fine. I can't buy an AC here, though.
Here is the code (remove it if you think it is inappropriate):
- Code: Select all
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
class Main {
void work() {
int nc = parseInt(readLine());
while (nc-- > 0) {
int n = parseInt(readLine());
String[] ss = new String[n];
for (int i = 0; i < n; i++) {
ss[i] = readLine();
}
Hashtable[] sets = new Hashtable[n];
for (int i = 0; i < n; i++) {
sets[i] = new Hashtable();
for (int j = 3; j <= 60; j++) {
for (int k = 0; k + j <= 60; k++) {
String sub = ss[i].substring(k, k + j);
sets[i].put(sub, "");
}
}
}
String res = "";
Enumeration e = sets[0].keys();
while (e.hasMoreElements()) {
String next = (String) e.nextElement();
boolean ok = true;
for (int i = 1; i < n; i++) {
if (!sets[i].containsKey(next)) {
ok = false;
break;
}
}
if (ok && next.length() > res.length())
res = next;
}
if (res.length() == 0)
res = "no significant commonalities";
System.out.println(res);
}
}
private String readLine() {
StringBuffer sb = new StringBuffer();
int b = 0;
while (b != '\n' && b >= 0) {
try {
b = System.in.read();
} catch (IOException e) {
return null;
}
if (b != '\r' && b != '\n' && b >= 0)
sb.append((char) b);
}
if (sb.length() == 0 && b < 0)
return null;
return sb.toString().trim();
}
private int parseInt(String s) {
int result = 0;
int sign = (s.charAt(0) == '-') ? -1 : 1;
if (sign == -1)
s = s.substring(1);
if (s.charAt(0) == '+')
s = s.substring(1);
int i = 0, max = s.length();
if (max > 0) {
while (i < max) {
result *= 10;
result += s.charAt(i++) - 48;
}
}
return sign * result;
}
public static void main(String args[]) {
Main myWork = new Main();
myWork.work();
}
}

