zoukankan      html  css  js  c++  java
  • acm专题---KMP模板

    KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m)

    觉得大话数据结果上面这个讲得特别好

    改进版本的KMP

    leetcode

    28. Implement strStr()

      

    class Solution {
    public:
        
        
        void getnext(string str,int next[])
    {
        int i=1;
        int j=0;
        next[1]=0;
        int str0=str.length()-1;
        while(i<=str0)
        {
            if(j==0||str[i]==str[j])
            {
                ++j;
                ++i;
                next[i]=j;
            }
            else
            {
                j=next[j];
            }
        }
        
    }
    int kmp(string s,string t)
    {
        int cnt=0;
        //int *next=new int[1000002];
        int next[1000002];
        getnext(t,next);
        int i=1;
        int j=1;
        int s0=s.length()-1,t0=t.length()-1;
        int idx=-1;
        while(i<=s0&&j<=t0)
        {
            if(j==0||s[i]==t[j])
            {
                ++i;
                ++j;
            }
            else
            {
                j=next[j];
            }
            if(j>t0)
            {
                cnt++;
                idx=i-t0-1;
                break;
                
            }
        }
        return idx;
    }
    
    int strStr(string haystack, string needle) {
        if(haystack.length()==0&&needle.length()<=haystack.length()||needle.length()==0) return 0;
        string tmpt="#",tmps="#";
        tmpt+=needle;
        tmps+=haystack;
        return kmp(tmps, tmpt);
            
    }
    };
    
    class Solution {
    public:
        
        void getnext(string str,int next[])
    {
        int i=1;
        int j=0;
        next[1]=0;
        int str0=str.length()-1;
        while(i<=str0)
        {
            if(j==0||str[i]==str[j])
            {
                ++j;
                ++i;
                if(str[i]!=str[j])
                    next[i]=j;
                else
                    next[i]=next[j];
            }
            else
            {
                j=next[j];
            }
        }
        
    }
    int kmp(string s,string t)
    {
        int cnt=0;
        //int *next=new int[1000002];
        int next[1000002];
        getnext(t,next);
        int i=1;
        int j=1;
        int s0=s.length()-1,t0=t.length()-1;
        int idx=-1;
        while(i<=s0&&j<=t0)
        {
            if(j==0||s[i]==t[j])
            {
                ++i;
                ++j;
            }
            else
            {
                j=next[j];
            }
            if(j>t0)
            {
                cnt++;
                idx=i-t0-1;
                break;
                
            }
        }
        return idx;
    }
    
    
    int strStr(string haystack, string needle) {
        if(haystack.length()==0&&needle.length()<=haystack.length()||needle.length()==0) return 0;
        string tmpt="#",tmps="#";
        tmpt+=needle;
        tmps+=haystack;
        return kmp(tmps, tmpt);
            
    }
    };
    

      

      

  • 相关阅读:
    MySQL 对于千万级的大表要怎么优化?
    随便写的一些docker使用操作命令
    零基础学python大概要多久?我用了30天
    普通人学python有意义吗?意义重大
    华为私有云组件
    Mysql 调优(二)分析思路
    MySQL 调优(一)调优原则
    shell脚本获取当前时间,分钟之前时间、小时之前时间和天之前时间
    java_windows环境变量自动设置脚本
    plsql中文乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/5625040.html
Copyright © 2011-2022 走看看