strstr函数

FZU - 2128 

问题很简单,给你一个字符串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

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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值