最长子串(FZU2128)
生活随笔
收集整理的這篇文章主要介紹了
最长子串(FZU2128)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最長子串
Time Limit:3000MS?????Memory Limit:65536KB?????64bit IO Format:%I64d & %I64u Submit?Status?Practice?FZU 2128Description
問題很簡單,給你一個字符串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 ellipseSample 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; }?
轉載于:https://www.cnblogs.com/yuyixingkong/p/4385714.html
總結
以上是生活随笔為你收集整理的最长子串(FZU2128)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Asp.net MVC 的一些总结(二)
- 下一篇: Lintcode: O(1) Check