HDU 1238 & POJ 1226 Substrings
来源:互联网 发布:java开发工作经历 编辑:程序博客网 时间:2024/06/08 11:14
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).
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 }
- HDU 1238 & POJ 1226 Substrings
- hdu 1238 & POJ 1226 Substrings
- poj 1226|| hdu 1238 Substrings(KMP)
- POJ 1226 && HDU 1238 Substrings(kmp)
- poj 1226 Substrings
- POJ 1226 Substrings KMP
- POJ 1226 Substrings
- poj 1226 Substrings
- poj 1226 Substrings
- poj 1226 Substrings
- POJ 1226 Substrings
- poj 1226:Substrings
- POJ 1226-Substrings
- poj-1226-Substrings
- POJ 1226 substrings
- poj 1226 Substrings
- poj 1226 Substrings
- POJ 1226 Substrings
- HDU 3236 Gft Hunting (多维背包)
- 三年学习编程学习总结,对新手有些帮助,我也在迷茫!
- HDU 3998 Sequence (最长上升子序列 + 最大流)
- 查看当前用户信息(权限、视图、表空间....)
- 每日雕蟲一技[2013-04-10]用Jquery删除一个数组中指定值的元素
- HDU 1238 & POJ 1226 Substrings
- POJ 3666 Making the Grade
- 对 LCS 和 LIS 的总结
- HDU 1506 Largest Rectangle in a Histogram
- HDU 1505 City Game
- RPi变成一个DHCP服务器
- SHGetFileInfo函数
- HDU 2830 Matrix Swapping II (最大子矩阵)
- HDU 2870 Largest Submatrix (最大子矩阵)