BUPT OJ 147. Substring

来源:互联网 发布:中科大苏州软件学院 编辑:程序博客网 时间:2024/06/11 02:33



时间限制 1000 ms 内存限制 65536 KB

题目描述

There're two strings S and T formed by English letters (lowercase and uppercase). You are required to find a longest substring R from string S, that string R can be generated only by string T (overlap is essential).
For example:
T = "AB" cannot generate "ABC" because T does not have character 'C';
T = "ABA" can generate "ABABA" and "ABABABA" but not "ABABAB";
T = "AB" cannot generate "ABAB".
 

输入格式

There are several test cases.
The first line of input contains an integer N which indicates the number of test cases. Each test case is formed by two lines. The first describes the string S, and the other describes T.
The length of each string will not exceed 106.

输出格式

For each test case, you should output the longest length of string R.

输入样例

3ABCABCABCABABABABAABABAABA

输出样例

35
S菊苣之KMP解法
其实我想的是HASH
#include<bits/stdc++.h>using namespace std;vector<int> find_substr(string pattern, string text){    int n=pattern.size();    vector<int> next(n+1,0);    for(int i=1; i<n; ++i){        int j=i;        while(j>0){            j=next[j];            if(pattern[j]==pattern[i]){                next[i+1]=j+1;                break;            }        }    }    vector<int> positions;    int m=text.size();    for(int i=0, j=0; i<m; i++){        if(j<n&&text[i]==pattern[j]){            j++;        }        else{            while(j>0){                j=next[j];                if(text[i]==pattern[j]){                    j++;                    break;                }            }        }        if(j==n){            positions.push_back(i-n+1);        }    }    return positions;}const int MAXN=1000005;char a[MAXN], b[MAXN];string A, B;vector<int> v;deque<pair<int,int> > q;int main(){    int t; cin>>t;    while(t--){        scanf("%s%s",a,b);        A=string(a); B=string(b);        v=find_substr(B,A);        int n=B.size(), m=A.size();        if(v.size()==0){            puts("0");            continue;        }        int ans=0;        while(!q.empty()) q.pop_back();        for(int i=0; i<v.size(); i++){            while(!q.empty()&&q.front().second+n<=v[i]){                q.pop_front();            }            int ret;            if(!q.empty()) ret=q.back().first-(q.back().second-v[i]);            else ret=n;            ans=max(ret,ans);            q.push_back(make_pair(ret,v[i]));        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击