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
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 }
相关文章推荐
- POJ 1226 && HDU 1238 Substrings(kmp)
- hdu 1238 & POJ 1226 Substrings
- poj 1226|| hdu 1238 Substrings(KMP)
- poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题
- (重要题目)POJ 1226 Substrings(…
- poj 1226, hdoj1238, zoj 1374 Substrings
- Substrings &&http://acm.hdu.edu.cn/showproblem.php?pid=1238
- Arbitrage(POJ-2240 && HDU-1217)
- HDU 1963 && POJ 2063
- HDU 1238 Substrings
- HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)
- Taxi Cab Scheme POJ && HDU
- POJ 1422 && HDU 1151 Air Raid(最小路径覆盖-hungary)
- POJ 1151 & HDU 1542 Atlantis (矩形切割)
- Hdu 2844 & Poj 1742 结题报告
- POJ 1087&&HDU 1526 A Plug for UNIX (二分图+Floyd传递闭包 )
- hdu 1238 Substrings(KMP)
- HDU 1238 Substrings(求公共正反向连续子串)
- HDU 1536 && POJ 2960 S-Nim SG函数
- HDU 1159 && POJ 1458【lcs】