zoukankan      html  css  js  c++  java
  • hdu 1358 KMP的应用

    网上好多说得不够清楚,不过真正理解了KMP的next数组的含义,一点就通了。非优化的next数组的含义是:next[i]=k表示模式串下标为i的字符的前k个字符与开头的前k个字符相等,那么从0到i-1的模式串必然是循环的,循环节为从0到next[i] - (2 * next[i] - i),其中 2 * next[i] - i表示的是从后往前的部分与从前往后的部分相交的长度。

    /*
    * hdu1358/linux.cpp
    * Created on: 2011-8-29
    * Author : ben
    */
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cmath>
    #include
    <algorithm>
    using namespace std;

    #define MAXN 1000005
    int N;
    char str[MAXN];
    int nextval[MAXN];

    void work();
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    work();
    return 0;
    }

    void get_nextval() {
    int i = 0, j = -1;
    int parlen = N;
    char *pattern = str;
    nextval[
    0] = -1;
    while (i < parlen) {
    if (j < 0 || pattern[i] == pattern[j]) {
    i
    ++;
    j
    ++;
    if (pattern[i] != pattern[j]) {
    nextval[i]
    = j;
    }
    else {
    nextval[i]
    = nextval[j];
    }
    }
    else {
    j
    = nextval[j];
    }
    }
    }

    void get_next() {
    int i = 0, j = -1;
    int parlen = N;
    char *pattern = str;
    nextval[
    0] = -1;
    while (i <= parlen) {
    if (j == -1 || pattern[i] == pattern[j]) {
    i
    ++;
    j
    ++;
    nextval[i]
    = j;
    }
    else {
    j
    = nextval[j];
    }
    }
    }

    void work() {
    int T = 0, tmp;
    while (scanf("%d", &N) == 1 && N > 0) {
    scanf(
    "%s", str);
    get_next();
    printf(
    "Test case #%d\n", ++T);
    for (int i = 2; i <= N; i++) {
    tmp
    = i - nextval[i];
    if (i % tmp == 0 && i / tmp > 1) {
    printf(
    "%d %d\n", i, i / tmp);
    }
    }
    putchar(
    '\n');
    }
    }
  • 相关阅读:
    大道至简读后感
    CodeWarrior10 如何复制Workspace配置到另一个workspace
    CCS 6新建文件自动生成注释
    C语言之标准头文件模板
    C语言之标准源文件模板
    正确理解c和c ++的复杂类型声明
    各种滤波算法的介绍及其实现
    函数的前部注释
    关于C语言头文件写法的探讨
    02_温湿度传感器
  • 原文地址:https://www.cnblogs.com/moonbay/p/2158446.html
Copyright © 2011-2022 走看看