最长子串 FZU2118

 

http://acm.fzu.edu.cn/problem.php?pid=2128

 

分析:利用strstr()函数将每个字串在原串中的首尾位置存储一下,再将首尾从小到大排一下序。(写着写着就感觉和看电视节目那一道题一样一样的啊~)

例子: aaaa  2  aa  aa  答案:1

       abc   1  d  答案:3 

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=1000009;
const int INF=0x3f3f3f3f;
const int mod=2009;

char str[maxn];
char mstr[110];

struct node
{
    int s, e;
} a[maxn];

int cmp(node p, node q)
{
    if(p.e != q.e)
        return p.e<q.e;

    return p.s<q.s;
}

int main()
{
    int n, k;
    while(scanf("%s", str)!=EOF)
    {
        scanf("%d", &n);

        k = 0;

        for(int i=0; i<n; i++)
        {
            scanf("%s", mstr);

            int cnt = 0;
            int len = strlen(mstr);
            while(strstr(str+cnt, mstr)!=NULL)
            {
                int p = strstr(str+cnt, mstr)-str;
                a[k].s = p;
                a[k++].e = p + len - 1;
                cnt = p + len - 1;
            }
        }

        a[k].s = 0;
        a[k++].e = strlen(str);

        if(k == 1)
        {
            printf("%d\n", strlen(str));
            continue;
        }

        sort(a, a+k, cmp);

        int maxs = -INF;
        for(int i=k-1; i>0; i--)
        maxs = max(maxs, a[i].e-a[i-1].s-1);

        printf("%d\n", maxs);
    }
    return 0;
}

/*
ab
1
c
*/
View Code

 

转载于:https://www.cnblogs.com/daydayupacm/p/5798336.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值