zoukankan      html  css  js  c++  java
  • Substrings(hd1238)

    Substrings

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8183    Accepted Submission(s): 3752


    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
    找到最短字符串,比如其长度为5,先取5的子串,再取4的子串...每次遍历其他字符串中是否含有其逆串或顺串
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 /* 原型:char * strncpy(char *dest, char *src, size_t n);   功能:将字符串src中最多n个字符复制到字符数组dest中(它并不像strcpy一样只有遇到NULL才停止复制,而是多了一个条件停止,就是说如果复制到第n个字符还未遇到NULL,也一样停止),返回指向dest的指针。*/
     6 int main()
     7 {
     8     int N,i,j,num;
     9     freopen("in.txt","r",stdin);
    10     cin>>N;
    11     while(N--)
    12     {
    13         char string[100][103],pos[103],inv[103],str[103];
    14         int min_str=100,index,len,flag=0;
    15         cin>>num;
    16         for(i=0;i<num;i++)
    17         {
    18             cin>>string[i];//相当于把
    换成'';
    19             if(strlen(string[i])<min_str)
    20                 min_str=strlen(string[i]);
    21             index=i;
    22         }//输入字符串,并找到短字符串
    23         len=min_str;
    24         strcpy(str,string[index]);
    25         while(len>0)
    26         {
    27             for(i=0;i<=min_str-len;i++)//对于len长的子串可以取多少种;从最长的开始取
    28             {
    29                 flag=1;//假设该子串符合
    30                 strncpy(pos,str+i,len);//并不会把''复制进去,自己加进去
    31                 for(j=0;j<len;j++)
    32                     inv[j]=pos[len-j-1];//求出逆向子串;
    33                 inv[len]=pos[len]='';
    34                 for(j=0;j<num;j++)
    35                 {
    36                     if(strstr(string[j],inv)==NULL&&strstr(string[j],pos)==NULL)
    37                     {
    38                         flag=0;
    39                         break;
    40                     }
    41                 }
    42                 if(flag)
    43                     break;
    44             }
    45             if(flag)
    46                 break;
    47             len--;
    48         }
    49         cout<<len<<endl;
    50     }
    51 }
  • 相关阅读:
    推荐影视
    《算法设计》一、引言:某些典型的问题
    使用google地图API
    Python导入不同文件夹下模块
    Pycharm之远程编程
    linux之scp
    Photoshop脚本之jpg转换成eps
    win7重新安装win7
    查看修改swap空间大小
    用 EasyBCD 在 Win7/8 中硬盘安装 Ubuntu
  • 原文地址:https://www.cnblogs.com/a1225234/p/4595290.html
Copyright © 2011-2022 走看看