HDU1238 Substrings(kmp,最长公共子串)

来源:互联网 发布:手机淘宝秒杀需要刷新 编辑:程序博客网 时间:2024/06/05 22:41

Problem Description

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

23ABCDBCDFFBRCD2roseorchid

Sample Output

22

思路

求很多串的最长公共子串,串可以翻转,和POJ3080差不多

代码

#include<cstdio>#include<cstring>#include<string>#include<set>#include<iostream>#include<stack>#include<queue>#include<vector>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 100+20#define ll longlongusing namespace std;int t,n;int nxt[N];string str[150];void get_next(string s){    int len=s.length();    int j=0,k=-1;    nxt[0]=-1;    while(j<len)        if(k==-1||s[j]==s[k])            nxt[++j]=++k;        else            k=nxt[k];}bool kmp(string p,string s)//判断s是不是p的子串{    int plen=p.length();    int slen=s.length();    int i=0,j=0;    while(i<plen&&j<slen)    {        if(j==-1||p[i]==s[j])        {            i++;            j++;        }        else            j=nxt[j];    }    if(j==slen)        return true;    return false;}int pd(string s){    int flag=0;    get_next(s);    for(int k=1; k<n; k++)        if(!kmp(str[k],s))        {            flag=1;            break;        }    if(!flag)        return 1;    else        return 0;}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    cin>>t;    while(t--)    {        cin>>n;        for(int i=0; i<n; i++)            cin>>str[i];        string ans="";        for(int i=1; i<=str[0].size(); i++)        {            for(int j=0; j<=str[0].size()-i; j++)            {                string op=str[0].substr(j,i);                if(pd(op))                {                    if(ans.size()<op.size())                        ans=op;                }                reverse(op.begin(),op.end());                if(pd(op))                {                    if(ans.size()<op.size())                        ans=op;                }            }        }        cout<<ans.size()<<endl;    }    return 0;}