A - Oulipo

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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 在刚开始用ci.get()一个个的输入字符,然后竟然超时了,然后就直接将一行输入,就ac了,可见每次单独的IO操作都会耗费一定的时间,而且耗费的时间还不短。
#include<iostream>
using namespace std;
char text[1000005];
char w[10005];
int nex[10005];
void findNext(int n)
{
    nex[0]=-1;
    int i=0;
    int k=-1;
    while(i<n)
    {
        if(-1==k || w[nex[i]]==w[i])
        {
            i++;
            k++;
            nex[i]=k;
        }
        else
        {
            k=nex[k];
        }
    }
}
int kmp(int n,int m)
{
    int i=0;
    int j=0;
    int num=0;
    while(i<n)
    {
        if(text[i]==w[j])
        {
            i++; j++;
        }
        else
        {
            j=nex[j];
        }
        if(j==m)
        {
            j=nex[j];
            num++;
        }
        if(-1==j)
        {
            j=0; i++;
        }
    }
    return num;
}
int main()
{
    int N;
    cin>>N;
    cin.get();
    int n=0,m=0;
    while(N--)
    {
        n=0;m=0;
        cin>>w;
        m=strlen(w);
        cin>>text;
        n=strlen(text);
        findNext(m);
        int num=kmp(n,m);
        cout<<num<<endl;
    } }

A - Oulipo的更多相关文章

  1. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...

  2. poj3461 Oulipo(KMP模板)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17795   Accepted: 7160 Descripti ...

  3. Match:Oulipo(POJ 3461)

     Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...

  4. KMP算法 hdu4686 Oulipo

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  5. hdu----1686 Oulipo (ac自动机)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  7. POJ 3461 Oulipo

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. Oulipo (kmp)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26857   Accepted: 10709 Descript ...

  9. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

随机推荐

  1. UIPikerView

    UIPikerView的属性 1.   numberOfComponents:返回UIPickerView当前的列数 NSInteger num = _pickerView.numberOfCompo ...

  2. ubuntu16.04 64位server安装php7

    You can do the following: sudo apt-get install python-software-properties sudo LC_ALL=C.UTF-8 add-ap ...

  3. Android Terminal telnet windows

    /******************************************************************************************** * Andr ...

  4. Heritrix源码分析(十三) Heritrix的控制中心(大脑)CrawlController(二)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/650744      本博客已迁移到本人独立博客: http://www.yun5u. ...

  5. 【转】使用oschina的git服务器

    原文网址:http://blog.csdn.net/zengraoli/article/details/24975551 1.概要 其实oschina的git服务器与github的差不多,不过既然是中 ...

  6. 【转】JavaSript模块规范 - AMD规范与CMD规范介绍

    JavaSript模块化   在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?       模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...

  7. MongoDB中shell基本使用

    1.创建一个数据库,名为foobar use foobar 这个foobar数据库没有被真正的被创建.只是在mongo的缓存中 但是直接离开的话,这个数据库会被mongo删除. 2.现在我们给foob ...

  8. Shell教程3-Shell特殊变量

    前面已经讲到,变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid,看下面的代码:   $echo $$ ...

  9. redo和undo的区别

    转摘:http://blog.163.com/jing_playboy/blog/static/757362222012520104521864/   redo--> undo-->dat ...

  10. android LinearLayout 实现两端对齐

    <?xml version="1.0″ encoding="utf-8″?> <LinearLayout xmlns:android="http://s ...