• 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 }
  • 相关阅读:
    Apache配置
    linux centos使用xrdp远程界面登陆
    实现台式机redhat6.4无线网卡上网RTL8188CUS
    linux-redhat6.4驱动无线网卡rtl8188eu
    linux开机自动连接无线网络
    javascript for循环 日期 select
    amazeui折叠面板智能化展开
    PHP Ueditor 富文本编辑器
    vue.js 使用小结
    php mysqli mysqli_query() mysqli_real_query()
  • 原文地址:https://www.cnblogs.com/a1225234/p/4595290.html
Copyright © 2020-2023  润新知