NEFU 721 Substrings exp 枚举

Substrings exp

Time Limit 1000ms

Memory Limit 65536K

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 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
							

-------------

从最短的字符串开始,从大到小枚举字串长度以及起点

-------------

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

int n;
char s[222][222];
char tmp[222];
char pmt[222];
int ans;
bool ok;
int main()
{
    int T,len,p;
    scanf("%d",&T);
    while (T--)
    {
        len=p=0;
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            if (strlen(s[i])>len)
            {
                len=strlen(s[i]);
                p=i;
            }
        }
        for (int L=len;L>=0;L--)
        {
            for (int i=0;i+L<=len;i++)
            {
                for (int k=0;k<L;k++)
                {
                    tmp[k]=s[p][i+k];
                }
                tmp[L]='\0';
                int ll=strlen(tmp);
                for (int g=0;g<ll;g++)
                {
                    pmt[g]=tmp[ll-g-1];
                }
                pmt[ll]='\0';
                ok=true;
                for (int j=1;j<=n;j++)
                {
                    if (strstr(s[j],tmp)==NULL&&strstr(s[j],pmt)==NULL)
                    {
                        ok=false;
                        break;
                    }
                }
                if (ok)
                {
                    ans=L;
                    break;
                }
            }
            if (ok) break;
        }
        printf("%d\n",ans);
    }
    return 0;
}




你可能感兴趣的:(题解)