A - Oulipo(4.6.2)

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 알고리즘 에 대한 구체 적 인 설명 은 이 블 로 그 를 보십시오.http://billhoo.blog.51cto.com/2337751/411486,http://blog.csdn.net/yaochunnian/article/details/7059486,http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html
    그들 은 매우 상세 하 게 말 했 고 KMP 알고리즘 은 이해 하기 가 매우 어 려 웠 다. 마음 을 잠재 우 고 자세히 연구 해 야 한다. 몇 시간 동안 그 의 미 를 진정 으로 이해 하지 못 했다.
    /*
     *POJ 3461
     *KMP   
     *  :      C++   。G++       。
     */
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    #define maxw 10005
    #define maxt 1000005
    
    void getnext(char *p, int *next)
    {
        int j, l, cur;
        next[0] = -1;   //  w     suffix[]    
        next[1] = 0;
        j = 0;
        l = strlen(p);
        for( cur = 2; cur <= l; cur++ )
        {
            while( j >= 0 && p[j] != p[cur - 1] )      //  kmp      w     
                j = next[j];
            next[cur] = ++j;
        }
    }
    int match( char *s, char *p, int *next)   //s      p      next     
    {
        int i, j, slen, plen, cnt;
    
        i = 0; j = 0; cnt = 0;
        slen = strlen(s);
        plen = strlen(p);
    
        while( i < slen )
        {
            if( j == -1 || s[i] == p[j] ) // s w            ,           ,        p    
                                          //s       p          
                i++, j++;
            else if( j >= 0 )
                j = next[j];       //     i   。     p     ,   next      p   
                                   //  s       p        
            if( j == plen )
                cnt++, j = next[j];
        }
        return cnt;
    }
    int main()
    {
        int n;
        char w[maxw], t[maxt];
        int suffix[maxw];
    
        cin >> n;
        while( n -- )
        {
            cin >> w >> t;
            getnext( w, suffix );
            cout << match( t, w, suffix) << endl;
        }
    }

    좋은 웹페이지 즐겨찾기