HDU--1238-Substrings(-最长连续子串)
2017-07-12 10:15
330 查看
Description
You are given anumber of case-sensitive strings of alphabetic characters, find the largeststring X, such that either X, or its inverse can be found as a substring of anyof the given
strings.
Input
The first lineof the input file contains a single integer t (1 <= t <= 10), the numberof test cases, followed by the input data for each test case. The first line ofeach test case
contains a single integer n (1 <= n <= 100), the number ofgiven strings, followed by n lines, each representing one string of minimumlength 1 and maximum length 100. There is no extra white space before and aftera string.
Output
There should beone 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
You are given anumber of case-sensitive strings of alphabetic characters, find the largeststring X, such that either X, or its inverse can be found as a substring of anyof the given
strings.
Input
The first lineof the input file contains a single integer t (1 <= t <= 10), the numberof test cases, followed by the input data for each test case. The first line ofeach test case
contains a single integer n (1 <= n <= 100), the number ofgiven strings, followed by n lines, each representing one string of minimumlength 1 and maximum length 100. There is no extra white space before and aftera string.
Output
There should beone 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
#include<stdio.h> #include<string.h> #include<math.h> #include<map> #include<algorithm> #include<iostream> #include<string> using namespace std; int main() { int t; char a[1005][1005],str[1005],restr[1005]; int l[1005]; cin>>t; while(t--) { int n,i,j; cin>>n; // printf("n=%d\n",n); for(i=0;i<n;i++) { scanf("%s",a[i]); l[i]=strlen(a[i]); // printf("a[%d]=%s\n",i,a[i]); } int max=0,min=99999,x,y; for(j=0;j<i;j++) { if(max<l[j]) { max=l[j]; x=j; } if(min>l[j]) { min=l[j]; y=j; } } // 4000 printf("max=%d,min=%d,a[%d]=%s,a[%d]=%s\n",max,min,min,a[y],max,a[x]); int q=min; int p=min; int o=1; int d; while(q>0) { for(i=0;i<=p-q;i++) { d=1; strncpy(str,a[y]+i,q); strncpy(restr,a[y]+i,q); str[q]=restr[q]='\0'; strrev(restr); // printf("str=%s,restr=%s\n",str,restr); for(j=0;j<n;j++) { if(strstr(a[j],str)==NULL&&strstr(a[j],restr)==NULL) d=0; } if(d==1) { printf("%d\n",strlen(str)); o=0; break; } } q--; if(o==0) break; } if(q==0&&d==0) printf("0\n"); } }
相关文章推荐
- poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题
- HDU 1238 Substrings(求公共正反向连续子串)
- HDU 1238 Substrings (最长公共子串+DFS)
- HDU 1238 最长子串
- HDU 6205 card card card【最长连续子串+尺取法】
- HDU 1003 Max Sum(最长连续子串和)
- HDOJ 1238 Substrings 【最长公共子串】
- Substrings - HDU 1238(最大共同子串)
- hdu 1238 Substrings(求子串或者他的逆序子串出现在每个字符串中)
- hdu 1003 求最长连续子串和
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
- HDU 3308 LCIS(线段树+区间合并+最长递增连续子串)
- HDU 1238 Substrings
- 最长连续三字符子串问题
- 求一个字符串中的最长连续子串
- 【DP+预处理|最长连续子序列】HDU-1506 Largest Rectangle in a Histogram
- HDU 3068 最长回文子串
- HDU 1238 Substrings
- JS使用正则表达式找出最长连续子串长度