D. Substrings

来源:互联网 发布:同步带传动计算软件 编辑:程序博客网 时间:2024/06/07 16:47
D.   Substrings

Time Limit: 1.0 Seconds   Memory Limit: 65536K



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 
这道题大概是做过最难的一道acm题了TAT
不仅要求正序寻找子串,还要逆过来,最后输出子串的最大长度,我实在是想不出来,就从网上搜答案,结果,发现全是c语言,没有c++
人生有多绝望。
其实这道题就是不断的的用循环结构去搜索子串,简称暴力搜索吧(班里的大佬说的)
#include <iostream>#include <string>#include <cstring>#include <cmath> using namespace std;int main(){    char str[100][100];//首先定义一个二维char类型的数组         char s1[100] ,s2[100];//定义两个数组,为之后存放子串         int mins , maxsc ,text ,len ,flag , num ,i;    cin >> text;//测试的次数         while (text--)    {        mins = 100;        maxsc = 0;        int n = 0;                cin >> n;//n是即将输入的的字符串的个数         for (int i = 0; i < n ; i++)        {            cin >> str[i];//输入字符串             len = strlen(str[i]);//cout << len;字符串的长度             if (len < mins)            {                mins = len;//令mins作为最短字符串的长度                 num = i ;//将最小字符串的脚码存进i;             }        }        for (int i = 0; i < mins ; i++)        {            for (int j = i ; j < mins ; j++)            {                flag = 1;                 for (int k = i; k <= j; k++)                {                    s1[k - i] = s2[ j - k ] = str[num][k]; //将最小字符串中的子串分别正序 逆序存进两个数组                 }                                  s1 [j - i + 1] =   s2 [j - i + 1] = '\0';                                   int len2 = strlen(s1) ;//cout << len <<endl;//寻找的子串的长度                                  for (int l = 0; l < n ; l++)                                {                    if ((!strstr(str[l],s1))&&(!strstr(str[l],s2)))//如果在其他字符串中都没有找到这个子串,跳出循环,再搜索其他子串                     {                        flag = 0;                        break;                    }                    else flag = 1;                                     }                if (flag)                {                    if (len2 > maxsc)                        maxsc = len2;//更新最大的字符串子串的长度                 }            }        }        cout << maxsc << endl;    }        return 0; }
原创粉丝点击