HDOJ_1238 Substrings 解题报告

这是一个关于HDOJ_1238题目的解题报告,主要探讨如何检查字符串数组中是否存在相同的子串。代码中通过查找最短字符串及其所有可能的子串,并使用strstr函数来确定这些子串是否存在于其他字符串中。最终输出最长公共子串的长度。
摘要由CSDN通过智能技术生成

 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
  
  
2 3 ABCD BCDFF BRCD 2 rose orchid
 

Sample Output
  
  
2 2
 
 
解题 报告: 先是找出里面长度最短的字符串,然后枚举出那个字符串的正向和逆向的字串,然后和他们对比,不过字串是从大到小找下去的,找到符合的即可了。
代码如下:
Code:
  1. #include<iostream>  
  2. #include<cstring>  
  3. using namespace std;  
  4. const int Max(105);  
  5.   
  6. char data[Max][Max];  
  7. char Temp_Data[Max];  
  8.   
  9. int Check_Data(int a,int b)  
  10. {  
  11.     for(int i=0;i<a;i++)  
  12.         if(i!=b&&!strstr(data[i], Temp_Data))     //其中strstr的功能是在串中查找指定字符串的第一次出现,如果没有就返回NULL  
  13.             return 0;  
  14.         return 1;  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     int num;  
  20.     cin>>num;  
  21.     int n;  
  22.     while(num--&&cin>>n)  
  23.     {  
  24.         int state=0,length=-1;  
  25.         for(int i=0;i<n;i++)  
  26.         {  
  27.             cin>>data[i];  
  28.             if(length==-1||strlen(data[i])<length)    //找出长度最小的串并标记位置  
  29.             {  
  30.                 length=strlen(data[i]);  
  31.                 state=i;  
  32.             }  
  33.         }  
  34. //      cout<<state<<endl;  
  35.         int temp=length,flag=0;         //flag用来标记是否已经找到了符合条件的最长子串  
  36.         while(temp)  
  37.         {  
  38.             //正向字符串的子串  
  39.             for(int i=0;i+temp<=length;i++)  
  40.             {  
  41.                 for(int j=i,a=0;a<temp;j++,a++)  
  42.                     Temp_Data[a]=data[state][j];  
  43.                 Temp_Data[a]='/0';  
  44. //              cout<<Temp_Data<<endl;  
  45.                 if(Check_Data(n,state))  
  46.                 {  
  47.                     flag=1;  
  48.                     break;  
  49.                 }  
  50.             }  
  51.             if(flag)  
  52.                 break;  
  53.             //逆向字符串的子串  
  54.             for(i=length-1;i-temp>=-1;i--)  
  55.             {  
  56.                 for(int j=i,a=0;a<temp;j--,a++)  
  57.                     Temp_Data[a]=data[state][j];  
  58.                 Temp_Data[a]='/0';  
  59.                 if(Check_Data(n,state))  
  60.                 {  
  61.                     flag=1;  
  62.                     break;  
  63.                 }  
  64. //              cout<<Temp_Data<<endl;  
  65.             }  
  66.             if(flag)  
  67.                 break;  
  68.             temp--;  
  69.         }  
  70.         cout<<temp<<endl;  
  71.     }  
  72.     return 0;  
  73. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值