zoukankan      html  css  js  c++  java
  • fzu Problem 2128 最长子串(KMP + strstr 经典好题)

     Problem Description

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

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2128

    *************************************************

    题意:

    分析:利用strstr()函数将每个字串在原串中的首尾位置存储一下,再将首尾从小到大排一下序。

    AC代码:

    判题oj炸了,,,wait...

    *:

     1 kmp代码:
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<stdlib.h>
     5 #define MAX(x,y)(x>y?x:y)
     6 const int MAXN=1000010;
     7 char mstr[MAXN];
     8 char str[110];
     9 struct Node{
    10     int s,e;
    11 };
    12 Node area[MAXN];
    13 int cmp(const void *a,const void *b){
    14     if((*(Node *)a).e!=(*(Node *)b).e)return (*(Node *)a).e-(*(Node *)b).e;
    15     else return (*(Node *)a).s-(*(Node *)b).s;
    16 }
    17 int p[110],top;
    18 void getp(){
    19     int i=0,j=-1;
    20     p[0]=-1;
    21     while(str[i]){
    22         if(j==-1||str[i]==str[j]){
    23             i++;j++;
    24             p[i]=j;
    25         }
    26         else j=p[j];
    27     }
    28 }
    29 void kmp(){
    30     getp();
    31     int i=0,j=0;
    32     while(mstr[i]){
    33         if(j==-1||mstr[i]==str[j]){
    34             i++;j++;
    35             if(!str[j])area[top].s=i-j,area[top++].e=i-1;
    36         }
    37         else j=p[j];
    38     }
    39 }
    40 int main(){
    41     int N;
    42     while(~scanf("%s",mstr)){
    43         top=0;
    44         scanf("%d",&N);
    45         for(int i=0;i<N;i++){
    46             scanf("%s",str);
    47             kmp();
    48         }
    49         int ans=0;
    50         int n=strlen(mstr),t=0,temp;
    51         area[top].s=n;area[top].e=n;
    52         qsort(area,top+1,sizeof(area[0]),cmp);
    53         //for(int i=0;i<=top;i++)printf("%d %d
    ",area[i].s,area[i].e);
    54         for(int i=0;i<=top;i++){
    55             temp=area[i].e-t;
    56             ans=MAX(ans,temp);
    57             t=area[i].s+1;//注意***** 
    58         }
    59         printf("%d
    ",ans);
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    java 面向对象(十八):包装类的使用
    java 面向对象(十七):单元测试方法
    rxjava2学习
    多线程-wait(),notify(),notifyAll()
    多线程-Thread.join()的运用
    66. Plus One
    理解Android View的事件传递机制
    78. Subsets
    程序员学习与成长的方法(转发)
    android查询天气demo,基于mvp+kotlin+rxjava2+retrofit2
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5803237.html
Copyright © 2011-2022 走看看