zoukankan      html  css  js  c++  java
  • POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461

    Oulipo
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 50450   Accepted: 20018

    Description

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

    Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

    Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

    So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A''B''C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

    Input

    The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

    • One line with the word W, a string over {'A''B''C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
    • One line with the text T, a string over {'A''B''C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

    Output

    For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

    Sample Input

    3
    BAPC
    BAPC
    AZA
    AZAZAZA
    VERDI
    AVERDXIVYERDIAN

    Sample Output

    1
    3
    0

    Source

    KMP模板:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <algorithm>
     5 #define INF 0x3f3f3f3f
     6 #define LL long long
     7 using namespace std;
     8 const int MAXN = 1e6+10;
     9 const int MAXW = 1e4+10;
    10 char W[MAXW], T[MAXN];
    11 int wlen, tlen;
    12 int nxt[MAXW];
    13 
    14 void get_Next()
    15 {
    16     int j = 0, k = -1;
    17     nxt[0] = -1;
    18     while(j < wlen){
    19         if(k == -1 || W[j] == W[k]){
    20             nxt[++j] = ++k;
    21         }
    22         else k = nxt[k];
    23     }
    24 }
    25 
    26 int KMP_count()
    27 {
    28     int ans = 0, j = 0;
    29     if(wlen == 1 && tlen == 1){
    30         if(W[0] == T[0]) return 1;
    31         else return 0;
    32     }
    33     get_Next();
    34     for(int now = 0; now < tlen; now++){
    35         while(j > 0 && T[now] != W[j])
    36             j = nxt[j];
    37         if(W[j] == T[now]) j++;
    38         if(j == wlen){
    39             ans++;
    40             j = nxt[j];
    41         }
    42     }
    43     return ans;
    44 }
    45 
    46 int main()
    47 {
    48     int T_case;
    49     scanf("%d", &T_case);
    50     while(T_case--){
    51         scanf("%s%s", &W, &T);
    52         wlen = strlen(W);
    53         tlen = strlen(T);
    54 
    55         printf("%d
    ", KMP_count());
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    struts2 文件上传
    struts2 前端显示错误信息
    struts2
    struts2 针对类型转换出错的处理
    Beats:使用filebeat传送多行日志multiline
    logstash的output插件
    logstash的mutate插件
    logstash中date的时间处理方式总结
    logstash更新gem源
    logstash的Multiline插件
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10011981.html
Copyright © 2011-2022 走看看