zoukankan      html  css  js  c++  java
  • poj 3461 Oulipo

    KMP算法,按书上说的写一遍,总是很别扭,后来才知道是数组开始问题,就是从“1”还是从“0”开始,废了很多脑力,又增几多白发,才把书上的从1开始改为从0开始。昨天我一直熬到半夜也找不到问题在哪儿,今天一下就过了,可见坚持还是有好处的。

     1 #include <stdio.h>
     2 #include <string.h>
     3 int next[10005];
     4 char T[10005],S[1000005];
     5 void getnext(char *t)
     6 {
     7     int i=0,j=-1,l = strlen(t);
     8     next[0] = -1;
     9     while(i < l)
    10         if(j == -1 || t[i]==t[j])
    11         {
    12             i++;j++;
    13             if(t[i] != t[j]) next[i] = j;
    14             else next[i] = next[j];
    15         }
    16         else j = next[j];
    17 }
    18 int find(char *s, char *t)
    19 {
    20     int i,j,lt,ls,cnt;
    21     i = cnt = 0;j = 0;
    22     lt = strlen(t);
    23     ls = strlen(s);
    24     while(i < ls && j < lt)
    25     {
    26         if(s[i] == t[j])
    27             i++,j++;
    28         else
    29         {
    30             j = next[j];
    31             if(j == -1)
    32                 j++,i++;
    33         }
    34         if(j == lt)
    35         {
    36             cnt++;
    37             j = next[j];
    38         }
    39     }
    40     return cnt;
    41 }
    42 int main()
    43 {
    44     int n;
    45     scanf("%d",&n);
    46     while(n--)
    47     {
    48         scanf("%s%s",T,S);
    49         getnext(T);
    50         printf("%d\n",find(S,T));
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    display:flex 布局之 骰子
    vue 生命周期
    vue webpack 懒加载
    后台管理页面基本布局
    模拟ie9的placeholder
    常用的功能封装 pool.js
    六位数字字母验证码
    CommonJs AMD CMD
    项目封版后的总结
    jq 回到顶部
  • 原文地址:https://www.cnblogs.com/lzxskjo/p/2482498.html
Copyright © 2011-2022 走看看