zoukankan      html  css  js  c++  java
  • hdu 1686 KMP算法

    题意:

      求子串w在T中出现的次数。

    kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cstdio>
    
    using namespace std;
    int lenp;
    int lens;
    void getnext(int *next, char *p)
    {
        int j = 0, k = -1;
        next[0] = -1;
        while(j < lenp)
        {
    
            if(k == -1 || p[j] == p[k])
            {
                j++;
                k++;
                next[j] = k;
            }
            else
                k = next[k];
        }
    }
    
    char s[1000100], p[10100];
    int next[10100];
    int main(void)
    {
        int N;
        scanf("%d", &N);
        while( N-- )
        {
    
            scanf("%s", p);
            scanf("%s", s);
            lens=(int)strlen(s);
            lenp=(int)strlen(p);
    
            getnext(next, p);
            int i = 0;
            int j = 0;
            int sum = 0;
    
            while(i < lens&&j<lenp)
            {
                //printf("%d %d %c %c
    ",i,j,s[i],p[j]);
                if( j == -1 || s[i] == p[j] )
                {
    
                    i++;
                    j++;
                }
                else
                {
                    j = next[j];
                }
                //printf("#%d %d %c %c
    ",i,j,s[i],p[j]);
                if( j >=lenp)
                {
                    sum++;
                    j = next[j];/////////!!!!!!!!!!
                    //printf("%d %c %c
    ",sum,s[i],p[j]);
                }
            }
            printf("%d
    ", sum);
        }
        return 0;
    }
    View Code
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    #define L1 1000005
    #define L2 10005
    
    int next[L2], len1, len2, res;
    char s[L1], p[L2];
    
    void get_next ()
    {
        int j = 0, k = -1;
        next[0] = -1;
        while (j < len2)
        {
            if (k == -1 || p[j] == p[k])
            {
                j++, k++;
                next[j] = k;
            }
            else k = next[k];
        }
    }
    void kmp (int pos)
    {
        int i = pos, j = 0;
        while (i < len1 && j < len2)
        {
            //printf("%d %d %c %c
    ",i,j,s[i],p[j]);
            if (j == -1 || s[i] == p[j])
            {
                i++, j++;
            }
            else j = next[j];
            //printf("#%d %d %c %c
    ",i,j,s[i],p[j]);
            if (j >= len2)
                    res++, j = next[j];    //神奇之处,效率大增
        }
    }
    int main()
    {
        int t;
        scanf ("%d", &t);
        while (t--)
        {
            res = 0;
            scanf ("%s%s", p, s);
            len1 = strlen (s);
            len2 = strlen (p);
            get_next ();
            kmp (0);
            printf ("%d
    ", res);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    kafka与Rocketmq的区别
    CentOS7 安装特定版本的Docker brady
    Postgresql Error : must be superuser to alter superusers.
    php 用redis实现购物车底层代码
    查找文件夹中包含某字符的文件和行数
    utabs 下划线在微信端不出来
    PHP的生成器yield处理大量数据杠杠
    direction: rtl;
    强制html以https格式访问引入文件
    uviewui 引入 easycom 不用每个页面都引入
  • 原文地址:https://www.cnblogs.com/XDJjy/p/3923097.html
Copyright © 2011-2022 走看看