zoukankan      html  css  js  c++  java
  • poj 3461 Oulipo,裸kmp

    传送门

    Oulipo
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 32373   Accepted: 13093

    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

     
     
    15266997 njczy2010 3461 Accepted 1736K 125MS G++ 1147B 2016-03-14 17:00:52
     
    题意:
    裸的kmp
     
    代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <map>
     4 #include <cstring>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 #define N 1000005
    10 #define ll long long
    11 
    12 int TT;
    13 char s[N];
    14 char t[N];
    15 int next[N];
    16 int ans;
    17 int l;
    18 int lt;
    19 
    20 void get_next()
    21 {
    22     int i,j;
    23     i = 0;
    24     j = -1;
    25     next[0] = -1;
    26     while(i<l)
    27     {
    28         if(j == -1 || s[i] == s[j]){
    29             i++;j++;next[i] = j;
    30         }
    31         else{
    32             j = next[j];
    33         }
    34     }
    35 }
    36 
    37 void kmp()
    38 {
    39     int i,j;
    40     i = 0;
    41     j = 0;
    42     while(i < lt)
    43     {
    44         if(j == -1 || t[i] == s[j]){
    45             if(j == l - 1){
    46                 ans ++;
    47                 j = next[j];
    48             }
    49             else{
    50                 i++;j++;
    51             }
    52         }
    53         else{
    54             j = next[j];
    55         }
    56     }
    57 }
    58 
    59 int main()
    60 {
    61     //freopen("in.txt","r",stdin);
    62     //freopen("out.txt","w",stdout);
    63     scanf("%d",&TT);
    64     while(TT--){
    65     //while(scanf("%I64d%I64d",&n,&k)!=EOF){
    66         ans = 0;
    67         scanf("%s",s);
    68         l = strlen(s);
    69         scanf("%s",t);
    70         lt = strlen(t);
    71         get_next();
    72         kmp();
    73         printf("%d
    ",ans);
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    【原创】大叔问题定位分享(21)spark执行insert overwrite非常慢,比hive还要慢
    【原创】大叔经验分享(14)spark on yarn提交任务到集群后spark-submit进程一直等待
    【原创】大叔问题定位分享(20)hdfs文件create写入正常,append写入报错
    【原创】大叔问题定位分享(19)spark task在executors上分布不均
    【原创】大数据基础之Spark(4)RDD原理及代码解析
    【原创】大叔问题定位分享(18)beeline连接spark thrift有时会卡住
    【原创】大叔问题定位分享(17)spark查orc格式数据偶尔报错NullPointerException
    【原创】大叔经验分享(13)spark运行报错WARN Utils: Service 'sparkDriver' could not bind on port 0. Attempting port 1.
    linux定时任务
    source导入错码解决办法
  • 原文地址:https://www.cnblogs.com/njczy2010/p/5276356.html
Copyright © 2011-2022 走看看