zoukankan      html  css  js  c++  java
  • 最长子串 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
    ", 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
    ", maxs);
        }
        return 0;
    }
    
    /*
    ab
    1
    c
    */
    View Code
  • 相关阅读:
    noip2018练习总结
    东方CannonBall (概率DP)
    数论
    逆序对
    USACO5.3 校园网Network of Schools(Tarjan缩点)
    USACO09FEB 改造路Revamping Trails(分层图模板)
    Comet OJ模拟赛 Day1
    Tarjan模板
    NOIP 天天爱跑步(树上差分)
    树上差分
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5798336.html
Copyright © 2011-2022 走看看