zoukankan      html  css  js  c++  java
  • [HDOJ1686]Oulipo

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686

    Oulipo

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7716    Accepted Submission(s): 3102


    Problem 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
     

    还是KMP,和上一个题一样。就当时熟练一下KMP。要小心的是这回匹配到最后一个字符,并且注意输入T以后要吸收掉 。还有目标串和模式串的位置,跟上一个题也不一样。

     
     
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 
    13 using namespace std;
    14 
    15 const int maxn = 1000010;
    16 int na, nb;
    17 char a[maxn];
    18 char b[maxn];
    19 int pre[maxn];
    20 
    21 void getpre(char *b, int *pre) {
    22     int j, k;
    23     pre[0] = -1;
    24     j = 0;
    25     k = -1;
    26     while(j < nb) {
    27         if(k == -1 || b[j] == b[k]) {//匹配
    28             j++;
    29             k++;
    30             pre[j] = k;
    31         }
    32         else {  //b[j] != b[k]
    33             k = pre[k];
    34         }
    35     }
    36 }
    37 
    38 int kmp() {
    39     int ans = 0;
    40     int i = 0;
    41     int j = 0;
    42     getpre(b, pre);
    43     while(i < na) {
    44         if(j == -1 || a[i] == b[j]) {
    45             i++;
    46             j++;
    47         }
    48         else {
    49             j = pre[j];
    50         }
    51         if(j == nb) {
    52             ans++;
    53         }
    54     }
    55     return ans;
    56 }
    57 
    58 int main() {
    59     int T;
    60     scanf("%d", &T);
    61     getchar();
    62     while(T--) {
    63         memset(pre, 0, sizeof(pre));
    64         gets(b);
    65         gets(a);
    66         na = strlen(a);
    67         nb = strlen(b);
    68         printf("%d
    ", kmp());
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    Windows 科研软件推荐
    有关Python 包 (package) 的基本知识
    《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记
    Coursera助学金申请模板
    《Using Databases with Python》 Week2 Basic Structured Query Language 课堂笔记
    Jupyter 解决单个变量输出问题
    解决 pandas 中打印 DataFrame 行列显示不全的问题
    《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记
    缓存击穿及解决方案
    jvm垃圾收集器
  • 原文地址:https://www.cnblogs.com/kirai/p/4754561.html
Copyright © 2011-2022 走看看