您的位置:首页 > 其它

HDU 1238 & POJ 1226 Substrings

2013-04-10 21:01 281 查看
Substrings

Time Limit: 2000/1000 MS (Java/Others)  

Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5413    Accepted Submission(s): 2411

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

思路:
(1)strlen(char *a):返回字符数组的长度,以'\0'结尾,且不包含'\0'。
(2)strcpy(char *a,char *b):将字符数组b复制给字符数组a;
(3)strncpy(char *a,char *b,int n):将字符数组b的前n个字符复制到字符数组a的前n个位置,比如a = "1111111", b = "00000", n = 3,则a = "0001111".显然,当n = strlen(b)时,相当于strcpy(char *a,char *b).
(4)strstr(char *a,char *b):在字符数组a里,匹配字符数组b,返回指向a的第一个与b匹配的指针。比如a = "baaabbb", b = "aaa",则返回aaabbb,否则若不能匹配,返回NULL.
(5)若字符数组a[10] = "12345",则a+1 = "2345".
这道题目首先找出最短的字符串minstr,然后从大到小枚举该字符串的子串,一一匹配所有的字符串,若都能匹配,该子串长度就是答案。
 




View Code

1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4 #include <algorithm>
5
6 using namespace std;
7
8 int n;
9 char str[105][105];
10
11 void convert(char *s)
12 {
13     char temp[105];
14     int len = (int)strlen(s);
15     for(int i=0; i<len; i++)
16         temp[i] = s[len-1-i];
17     temp[len] = '\0';
18     strcpy(s,temp);
19     return;
20 }
21
22 int find(char *s)
23 {
24     int sublen = (int)strlen(s),len = (int)strlen(s);
25     char substr[105],resubstr[105];
26     while(sublen > 0)
27     {
28         for(int i=0; i<=(len - sublen); i++)
29         {
30             strncpy(substr,s+i,sublen);
31             strncpy(resubstr,s+i,sublen);
32             substr[sublen] = resubstr[sublen] = '\0';
33             convert(resubstr);
34             bool flag = true;
35             for(int j=1; j<=n; j++)
36             {
37                 if(strstr(str[j],substr) == NULL && strstr(str[j],resubstr) == NULL)
38                 {
39                     flag = false;
40                     break;
41                 }
42             }
43             if(flag)
44                 return sublen;
45         }
46         sublen --;
47     }
48     return 0;
49 }
50
51 int main()
52 {
53     int Case ;
54     scanf("%d",&Case);
55     while(Case--)
56     {
57         scanf("%d",&n);
58         int minlen = 0xfffffff;
59         char minstr[105];
60         for(int i=1; i<=n; i++)
61         {
62             scanf("%s",str[i]);
63             if((int)strlen(str[i]) < minlen)
64             {
65                 minlen = (int)strlen(str[i]);
66                 strcpy(minstr,str[i]);
67             }
68         }
69         int ans = find(minstr);
70         printf("%d\n",ans);
71     }
72     return 0;
73 }


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: