Problem H 最长子串

 Problem H 最长子串
 
http://acm.fzu.edu.cn/contest/problem.php?cid=129&sortid=8

Accept: 31    Submit: 110
Time Limit: 3000 mSec    Memory Limit : 65536 KB

 Problem 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

 Source

福州大学第十届程序设计竞赛
Cached at 2013-04-29 15:35:11.
 
 
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

char s[1000100];
char t[1010][110];
int cnt;

struct node{
    int x,y;
}num[1000100];

void Solve(char *a,char *b){
    int loc=0;
    int len=strlen(b);
    while(strstr(a+loc,b)!=NULL){
        num[cnt].x=strstr(a+loc,b)-a;
        num[cnt].y=num[cnt].x+len-1;
        loc=num[cnt].x+len-1;
        cnt++;
    }
}

int cmp(node aa,node bb){
    return aa.x<bb.x;
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%s",s)){
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s",t[i]);
        cnt=0;
        for(int i=0;i<n;i++){
            Solve(s,t[i]);
        }
        if(cnt==0){
            printf("%d\n",strlen(s));
            continue;
        }
        num[cnt].x=strlen(s);
        num[cnt].y=strlen(s);
        cnt++;
        sort(num,num+cnt,cmp);
        int ans=0;
        for(int i=0;i<cnt-1;i++){
            int tmp=num[i+1].y-(num[i].x+1);
            if(tmp>ans)
                ans=tmp;
        }
        printf("%d\n",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3050838.html