试题 算法提高 Substrings
问题描述
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
输入格式
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
输出格式
There should be one line per test case containing the length of the largest string found.
样例输入
2
3
ABCD
BCDFF
BRCD
2
rose
orchid
样例输出
2
2
题解
问题就是在给出的n个字符串中找到长度最长的子串,颠倒的也算。
package ADV;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
/**
* Substrings
* http://lx.lanqiao.cn/problem.page?gpid=T612
* AC
*
*/
public class ADV316 {
static ADV316 THIS = new ADV316();
static String[] s;
static int t,n;
static LinkedList<S> queue;
static HashMap<String, Integer> status;//保存这个子串状态 不符合条件:0/符合条件:1
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine().split(" ");
t = Integer.valueOf(s[0]);
for (int i = 0; i < t; i++) {
s = in.readLine().split(" ");
n = Integer.valueOf(s[0]);
ArrayList<String> strs = new ArrayList<>();
status = new HashMap<>();
int shortest = 0x3f3f3f3f;
int maxSize = 0;
int shortestCount = 0;
for (int j = 0; j < n; j++) {
String temp = in.readLine();
if(shortest>temp.length()) {//找最短的字符串
shortest = temp.length();
shortestCount = j;
}
strs.add(temp);
}
//以最小字符串的每个字符为目前的子串,慢慢增加。小的子串不符合就记录下来(剪枝)
String shortestStr = strs.get(shortestCount);//最短的字符串
queue = new LinkedList<ADV316.S>();
for (int j = 0; j < shortestStr.length(); j++) {
queue.addLast(THIS.new S(j,j+1));
}
while(!queue.isEmpty()) {//bfs
S s = queue.removeFirst();
String subStr = shortestStr.substring(s.front, s.last);
if(status.containsKey(subStr)) {//该子串分析过
int stat = status.get(subStr);
if(stat==1) {
if(s.last+1<=shortestStr.length())
queue.addLast(THIS.new S(s.front,s.last+1));
continue;
}else {//子串不符合条件
continue;
}
}
//该子串没分析过
String RevSubStr = new String(new StringBuilder(subStr).reverse());//反转字符串
int j = 0;
for (; j < strs.size(); j++) {
String temp = strs.get(j);
if(!temp.contains(subStr) &&!temp.contains(RevSubStr))
break;
}
if(j<strs.size()) {//不是所有字符串的子串
status.put(subStr, 0);
status.put(RevSubStr, 0);
continue;
}else {
status.put(subStr, 1);
status.put(RevSubStr, 1);
if(s.last+1<=shortestStr.length())
queue.addLast(THIS.new S(s.front,s.last+1));
maxSize = Math.max(maxSize, subStr.length());
}
}
System.out.println(maxSize);
}
}
class S{
int front;//记录子串在原串的前后位置
int last;
public S(int front, int last) {
this.front = front;
this.last = last;
}
}
}