问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长。
输入包含多组数据。第一行为字符串s,字符串s的长度1到10^6次方,第二行是字符串s不能包含的子串个数n,n<=1000。接下来n行字符串,长度不大于100。
字符串由小写的英文字符组成。
最长子串的长度
lgcstraightlalongahisnstreet 5 str long tree biginteger ellipse
12
strstr(s1,s2) 如果s2是s1的子串 返回s2在s1的头地址 如果没找到 返回NULL
+
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include<functional>
#define INF 0xfffffff
#define eps 1e-6
#define LL long long
using namespace std;
const int maxn=1e3+5;
const int maxx=1000005;
const double q = (1 + sqrt(5.0)) / 2.0; // 黄金分割数
struct node
{
int st,ed;
}Q[maxx];
int cmp(node a,node b)
{
return a.ed<b.ed;
}
char s1[maxx],s[maxn/10];
int main()
{
int n;
while(scanf("%s",s1)!=EOF)
{
cin>>n;
int cnt=0;
int len=strlen(s1);
for(int i=0;i<n;i++)
{
cin>>s;
int p=0;
int len1=strlen(s);
while(strstr(s1+p,s)!=NULL)
{
int q=strstr(s1+p,s)-s1;
Q[cnt].st=q;
Q[cnt++].ed=q+len1-1;
p=q+len1-1;
//cout<<<<"______"<<endl;
}
}
Q[cnt].st=len;Q[cnt++].ed=len;
sort(Q,Q+cnt,cmp);
//for(int i=0;i<cnt;i++)
// cout<<Q[i].st<<"WPX"<<Q[i].ed<<endl;
int ans=-1;
for(int i=0;i<cnt-1;i++)
{
int tmp=Q[i+1].ed-Q[i].st-1;
if(tmp>ans)
ans=tmp;
//cout<<ans<<"_____"<<endl;
}
if(ans==-1) cout<<len<<endl;
else cout<<ans<<endl;
}
}