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 }
  • 相关阅读:
    linux基础命令之一
    Chrome 控制台使用大全
    移动端效果 — 页面引入在线视频
    移动端——简单计分表单
    JS操作cookie
    移动端页面字体——rem的使用
    Highcharts 使用总结
    CSS水平居中
    python学习 day2
    python学习 day1
  • 原文地址:https://www.cnblogs.com/lzxskjo/p/2482498.html
Copyright © 2011-2022 走看看