zoukankan      html  css  js  c++  java
  • Match:Oulipo(POJ 3461)

                    

                      Oulipo

      题目大意:给你一个字符串,要你找到字符串包含指定子串的个数

      只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧

      

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <functional>
     4 #include <string.h>
     5 
     6 using namespace std;
     7 
     8 typedef int Position;
     9 static char Text[1000002], Word[10002];
    10 static int Next[10003];
    11 
    12 void KmpSearch(const int, const int);
    13 void GetNext(const int);
    14 
    15 int main(void)
    16 {
    17     int case_sum;
    18     scanf("%d", &case_sum);
    19     getchar();
    20 
    21     while (case_sum--)
    22     {
    23         scanf("%s", Word);
    24         scanf("%s", Text);
    25         KmpSearch(strlen(Word), strlen(Text));
    26     }
    27     return EXIT_SUCCESS;
    28 }
    29 
    30 void KmpSearch(const int w_len, const int t_len)
    31 {
    32     Position i = 0, j = 0;
    33     int k_count = 0;
    34     GetNext(w_len);
    35 
    36     while (i < t_len)
    37     {
    38         if (j == -1 || Text[i] == Word[j])
    39         {
    40             i++;
    41             j++;
    42             if (j == w_len)
    43             {
    44                 k_count++;
    45                 j = Next[j];
    46             }
    47         }
    48         else j = Next[j];
    49     }
    50     printf("%d
    ", k_count);
    51 }
    52 
    53 void GetNext(const int w_len)
    54 {
    55     Position i = 0, k = -1;
    56     Next[0] = -1;
    57 
    58     while (i < w_len)
    59     {
    60         if (k == -1 || Word[i] == Word[k])
    61         {
    62             i++;
    63             k++;
    64             Next[i] = Word[i] != Word[k] ? k : Next[k];
    65         }
    66         else k = Next[k];
    67     }
    68 }

      

  • 相关阅读:
    max()和数组里面的max
    NYOJ 超级台阶
    NYOJ Fibonacci数
    floor()向下取整函数
    pow()函数
    HDU 小数化分数 1717
    大端和小端存储
    字节对齐
    CvvImage内存泄漏解决
    01矩阵中,把0的点的行和列都置零
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5180865.html
Copyright © 2011-2022 走看看