Substrings HDU - 1238 (KMP+暴力枚举)

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86591482

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. 

Input

The first line of the input file 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. 

Output

There should be one line per test case containing the length of the largest string found. 

Sample Input

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2

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. 

Input

The first line of the input file 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. 

Output

There should be one line per test case containing the length of the largest string found. 

Sample Input

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2

题目大意:就是给你n个字符串,找出它们的公共最长字符串是多少。

解题思路:看到数据量你就懂了,暴力第一个字符串的每一个子串就可以了,每次都与其他的字符串KMP匹配就行了,需要注意的是对于子串需要反过来判断一下,因为题目说反过来存在也可以。

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 100 + 10 ;
const int INF = 0x3f3f3f3f ;
const ull seed = 133 ;
const int MOD = 10007 ;
const double PI = acos(-1.0) ;

int Next[Maxn], slen, plen ,n;
string a[Maxn],ptr,str ;

void get_next (){
    Next[0] = -1;
    int j = 0, k = -1;
    while (j < slen){
        if (k == -1 || str[j] == str[k]){
            Next[++j] = ++k;
        }
        else k = Next[k];
    }
}

bool kmp (){
    int i = 0, j = 0;
    while (i < plen){
        if (j == -1 || str[j] == ptr[i]){
            i++;
            j++;
        }
        else j = Next[j];
        if (j == slen) return true;
    }
    return false ;
}

int main (){
    int T;
    cin >> T;
    while (T--){
        cin >> n;
        for (int i = 0 ;i < n; i++){
            cin >> a[i];
        }
        int ans = 0;
        for (int i = 1; i < a[0].size(); i++){
            for (int j = 0 ; j <= a[0].size() - i; j++){
                str = a[0].substr(j,i);
//                cout << j << " " << i << " " ;
//                cout << str << endl ;
                slen = str.size();
                get_next();
                bool flag = 1;
                for (int k = 1; k < n; k++){
                    ptr = a[k];
                    plen = a[k].size();
                    if (kmp()) continue ;
                    reverse(ptr.begin(),ptr.end());
                    if (kmp()) continue ;
                    flag = 0;
                    break;
                }
                if (flag) ans = max(ans,slen);
            }
        }
        cout << ans << endl ;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86591482