蓝桥杯 Substrings Java

试题 算法提高 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;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值