Description
问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长。
Input
输入包含多组数据。第一行为字符串s,字符串s的长度1到10^6次方,第二行是字符串s不能包含的子串个数n,n<=1000。接下来n行字符串,长度不大于100。
字符串由小写的英文字符组成。
Output
最长子串的长度
Sample Input
lgcstraightlalongahisnstreet
5
str
long
tree
biginteger
ellipse
Sample Output
12
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std;char str[1000005]; int len; struct kode {char s[105]; } a[1005];struct node {int s,e; } b[1000005];int cmp(node a,node b) {return a.e<b.e; }int main() {int n,ans;while(~scanf("%s",str)){int i,j,k;scanf("%d",&n);len = 0;for(i = 0; i<n; i++){scanf("%s",a[i].s);int pos = 0;int l = strlen(a[i].s);while(strstr(str+pos,a[i].s)!=NULL){int f = strstr(str+pos,a[i].s)-str;b[len].s=f;b[len].e=f+l-1;len++;pos = f+l-1;// printf("%d %s\n",f,str+pos); }}b[len].s = b[len].e = strlen(str);len++;sort(b,b+len,cmp);ans = -1;// printf("len = %d\n",len);for(i = 1; i<len; i++){int tem = b[i].e-b[i-1].s-1;// printf("%d\n",tem);ans = max(ans,tem);}if(ans == -1)printf("%d\n",strlen(str));elseprintf("%d\n",ans);}return 0; }