HDU 1686 (字符串哈希初步)

Oulipo

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


 

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

Source

华东区大学生程序设计邀请赛_热身赛

题意:给你一个字符串 w ,一个字符串 t, 求 t 中 w 出现的次数。

思路:算出 w 的哈希值。算出 t 的前缀哈希值,然后枚举起点,在判断哈希值是否相等。

emmm,也给自己记录一下  开始的理解,前缀哈希值来算中间的子串哈希值。。。

假如一个字符串 s = "1234" , 另一个字符串是 t = "34" (下标从1开始),取  p = 10。  /// p == 10, 只是用来类比, 一般都取质数

所以依次会得到 s 的哈希值,  hash[1] = 1, hash[2] = 12, hash[3] = 123, hash[4] = 1234.

然后 t 的哈希值为  34    /// t 直接算总的哈希值就行了。

然后会发现 t 的哈希值   34 = hash[4] - hash[2] * 100.. ,100 也就是 10^(t的长度)   这样也就算出来了 s 串中 "34" 的哈希值。

/// 所以代码中,我们会有一个 powp 数组,就是记录 p 的次方。。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef unsigned long long ull;
const int maxn = 1e4 + 10;
const int p = 13331;
char w[maxn];
char t[maxn*100];
ull powp[maxn];
ull harsh[maxn*100];
ull harshw;

void init(){
    powp[0] = 1;
    for(int i = 1;i < maxn;i ++)
        powp[i] = powp[i-1] * p;
}

ull geth(char *s){
    ull h = 0;
    int len = strlen(s);
    for(int i = 0;i < len;i ++)
        h = h * p + s[i];
    return h;
}

void gethash(char *s){
    int len = strlen(s);
    harsh[0] = 0;
    for(int i = 0;i < len;i ++)
        harsh[i+1] = harsh[i] * p + s[i];
}

int main()
{
    int cas; scanf("%d",&cas);
    init();
    while(cas --){
        scanf("%s %s",w + 1,t + 1);
        harshw = geth(w + 1);
        gethash(t + 1);
        int lenw = strlen(w + 1);
        int lent = strlen(t + 1);
        int ans = 0;
        for(int i = 0;i + lenw <= lent;i ++){
            ull tmp = harsh[i+lenw] - harsh[i] * powp[lenw];
            if(tmp == harshw)
                ans ++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/81910326