hdu 1238 Substrings

来源:互联网 发布:手机号码数据库 编辑:程序博客网 时间:2024/06/10 07:53

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6729    Accepted Submission(s): 3004

 

 

Problem Description

You are given a number of case-sensitivestrings of alphabetic characters, find the largest string X, such that eitherX, or its inverse can be found as a substring of any of the given strings.

 

 

Input

The first line of the input file contains asingle integer t (1 <= t <= 10), the number of test cases, followed bythe input data for each test case. The first line of each test case contains asingle integer n (1 <= n <= 100), the number of given strings, followedby n lines, each representing one string of minimum length 1 and maximum length100. There is no extra white space before and after a string.

 

 

Output

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

 

 

Sample Input

2

3

ABCD

BCDFF

BRCD

2

rose

orchid

 

 

Sample Output

2

2

 

 

Author

Asia 2002, Tehran (Iran), Preliminary

 

题解:

这道题是求在多个字符串中找到最长子序列的长度,这道题用的是列举最短的字符串的每一个子字符串,要列举要用两层for循环,而和每一个字符串比较则需要一个循环,所以一共是3个循环完成列举。

源代码:

#include <iostream>

#include <string.h>

#include <stdio.h>

using namespacestd;

 

int t,n;

char str[105][105];

char mstr[105];

int maxlen;

bool flag;

 

void strreverse(char*source,intlen)

{

      chartemp;

 

      for(inti = 0;i <len/2;i++)

      {

           temp= source[i];

           source[i]=source[len-i-1];

           source[len-i-1]= temp;

      }

      source[len]='\0';

}

void strmaxlen()

{

      charsource[105],resource[105];

      for(inti = maxlen;i > 0;i--)

           for(intj = 0;j <= maxlen - i;j++)

           {

                 strncpy(source,mstr+j,i);

                 strncpy(resource,mstr+j,i);

                 strreverse(resource,i);

                 source[i]= '\0';

                 source[i]= '\0';

                 flag= true;

 

                 for(intk = 0;k < n;k++)

                      if(strstr(str[k],source)==NULL && strstr(str[k],resource)== NULL)

                      {

                            flag= false;

                            break;

                      }

                 if(flag)

                 {

                      maxlen= i;

                      return;

                 }

           }

      maxlen =0;

}

int main()

{

      cin>> t;

     

      for(intx = 0;x < t;x++)

      {

           cin>> n;

           maxlen= 0xffffff;

          

           flag= true;

           intl;

           for(inti = 0;i < n;i++)

           {

                 scanf("%s",str[i]);

                 l= strlen(str[i]);

                 if(l< maxlen)

                 {

                      maxlen= l;

                      strcpy(mstr,str[i]);

                 }

           }

 

           strmaxlen();

           printf("%d\n",maxlen);

      }

 

      return0;

}

0 0
原创粉丝点击