zoukankan      html  css  js  c++  java
  • POJ 3461 Oulipo

    Oulipo
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 36424   Accepted: 14721

    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

    1570. [POJ3461]乌力波

    ★☆   输入文件:oulipo.in   输出文件:oulipo.out   简单对比
    时间限制:1 s   内存限制:256 MB

    【题目描述】

    法国作家乔治·佩雷克(Georges Perec,1936-1982)曾经写过一本书,《敏感字母》(La disparition),全篇没有一个字母‘e’。他是乌力波小组(Oulipo Group)的一员。下面是他书中的一段话:

       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…

    佩雷克很可能在下面的比赛中得到高分(当然,也有可能是低分)。在这个比赛中,人们被要求针对一个主题写出甚至是意味深长的文章,并且让一个给定的“单词”出现次数尽量少。我们的任务是给评委会编写一个程序来数单词出现了几次,用以得出参赛者最终的排名。参赛者经常会写一长串废话,例如500000个连续的‘T’。并且他们不用空格。

    因此我们想要尽快找到一个单词出现的频数,即一个给定的字符串在文章中出现了几次。更加正式地,给出字母表{'A','B','C',...,'Z'}和两个仅有字母表中字母组成的有限字符串:单词W和文章T,找到W在T中出现的次数。这里“出现”意味着W中所有的连续字符都必须对应T中的连续字符。T中出现的两个W可能会部分重叠。

    【输入格式】

    输入包含多组数据。

    输入文件的第一行有一个整数,代表数据组数。接下来是这些数据,以如下格式给出:

    第一行是单词W,一个由{'A','B','C',...,'Z'}中字母组成的字符串,保证1<=|W|<=10000(|W|代表字符串W的长度)

    第二行是文章T,一个由{'A','B','C',...,'Z'}中字母组成的字符串,保证|W|<=|T|<=1000000。

    【输出格式】

    对每组数据输出一行一个整数,即W在T中出现的次数。

    【样例输入】

    3

    BAPC

    BAPC

    AZA

    AZAZAZA

    VERDI

    AVERDXIVYERDIAN

    【样例输出】

    1

    3

    0

    /*KMP算法*/
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define N 2100010
    int T,lens,lenp,Next[N];
    char s[N],p[N];
    void get_next(){
        int i=0,j=-1;
        Next[i]=j;
        while(i<lenp){
            if(j==-1||p[i]==p[j]){
                i++;j++;
                Next[i]=j;
            }
            else j=Next[j];
        }
    }
    int kmp(){
        get_next();
        int i=0,j=0,ans=0;
        while(i<lens&&j<lenp){
            if(j==-1||s[i]==p[j]){
                i++;j++;
            }
            else j=Next[j];
            if(j==lenp) ans++,j=Next[j];
        }
        return ans;
    }
    int main(){//kmp求子串出现次数 
        scanf("%d",&T);
        while(T--){
            scanf("%s%s",p,s);
            lens=strlen(s);
            lenp=strlen(p);
            printf("%d
    ",kmp());
        }
        return 0;
    }
  • 相关阅读:
    Java static keyword
    Final Keyword In Java
    Underscore template
    Query.extend() 函数详解-转载
    js闭包for循环总是只执行最后一个值得解决方法
    mui scroll和上拉加载/下拉刷新
    mui 手势事件配置
    118. 杨辉三角
    [ 周赛总结 ] 第 185 场力扣周赛
    55. 跳跃游戏
  • 原文地址:https://www.cnblogs.com/xiaoqi7/p/5860137.html
Copyright © 2011-2022 走看看