zoukankan      html  css  js  c++  java
  • HDU 1686 Oulipo

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 char a[1000100];
     8 char b[10100];
     9 int Len_a, Len_b, sum;
    10 int Next[10100];
    11 
    12 void GetNext()
    13 {
    14     int i = 0, j = Next[0] = -1;
    15     while (i < Len_b)
    16     {
    17         if (j == -1 || b[i] == b[j])
    18         {
    19             i++;
    20             j++;
    21             Next[i] = (b[i] == b[j] ? Next[j] : j);
    22         }
    23         else
    24             j = Next[j];
    25     }
    26 }
    27 
    28 void KMP()
    29 {
    30     int i = 0, j = 0;
    31     while (i < Len_a)
    32     {
    33         if (j == -1 || a[i] == b[j])
    34         {
    35             i++;
    36             j++;
    37             if (j == Len_b)
    38             {
    39                 sum++;
    40                 j = Next[j];
    41             }
    42         }
    43         else
    44             j = Next[j];
    45     }
    46 }
    47 
    48 int main(void)
    49 {
    50     ios::sync_with_stdio(false);
    51     
    52     int cas;
    53     cin >> cas;
    54     while (cas--)
    55     {
    56         cin >> b >> a;
    57         Len_a = strlen(a);
    58         Len_b = strlen(b);
    59         GetNext();
    60         sum = 0;
    61         KMP();
    62         cout << sum << endl;
    63     }
    64     
    65     return 0;
    66 }
    不忘初心,方得始终
  • 相关阅读:
    Mac上安装使用Nginx
    突然想到了王自如
    sqls
    spring利用后置处理器初始化bean属性
    replace的用法
    Redis使用记录
    netty handlers模式
    webmagic 下载页面
    webmagic 初始化 startRequests
    Singleton模式
  • 原文地址:https://www.cnblogs.com/ducklu/p/8921856.html
Copyright © 2011-2022 走看看