HDU5763 Another Meaning(dp+hash)

HDU5763 Another Meaning

As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”.
Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to ??. ?? is so smart that he knows the word B in the sentence has two meanings. He wants to know how many kinds of meanings MeiZi can express.

Input

The first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two strings A and B, A means the sentence MeiZi sends to ??, B means the word B which has two menaings. string only contains lowercase letters.
Limits
T <= 30
|A| <= 100000
|B| <= |A|

Output

For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 1000000007.

Sample Input

4
hehehe
hehe
woquxizaolehehe
woquxizaole
hehehehe
hehe
owoadiuhzgneninougur
iehiehieh

Sample Output

Case #1: 3
Case #2: 2
Case #3: 5
Case #4: 1

Hint

In the first case, “ hehehe” can have 3 meaings: “*he”, “he*”, “hehehe”.
In the third case, “hehehehe” can have 5 meaings: “*hehe”, “he*he”, “hehe*”, “**”, “hehehehe”.

题解

题意

给出两个字符串。其中第一个字符串为要处理的字符串,第二个字符串为解释字符串,也就是说,对于第二个字符串,其有两种意思。问第一个字符串能被解释成几种意思?

思路

首先,必不可少的就是查找到字符串所在的位置。当查找到位置后,下一步的操作便是对可能的情况进行选择。我们了解到对于第i位的情况,其含义只能由i-1位或者i-m位转移过来。其中m为第二个字符串的长度,所以可以得到递推公式:

dp[i] = dp[i-1] 当前位置不为第二字符串结尾

dp[i] = dp[i-1]+dp[i-m] 当前位置为第二字符串结尾

代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

#define REP(i,n) for(int i=0;i<(n);i++)

const int MAXN = 1e6+10;
const ll mod = 1e9+7;
const ull hnm = 157;

char a[MAXN],b[MAXN];
ll dp[MAXN];
ll lena,lenb;
ull x[MAXN];
ull sav_hash[MAXN];
ull hashb =  0;


void init(){
    memset(dp,0,sizeof(dp));
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    x[0]=1;
    for(int i=1;i<MAXN;i++){
        x[i] = x[i-1]*hnm;
    }
    dp[0]=1;
}

ull id(char a){
    return a-'a'+1; 
}

void cal(){
    hashb = 0;
    for(int i=1;i<=lenb;i++){
        hashb = hashb*hnm + id(b[i]);
    }
    for(int i=1;i<=lenb;i++){
        sav_hash[i] = sav_hash[i-1]*hnm+id(a[i]);
    }
    for (int i = lenb+1; i <= lena; i++) {
        sav_hash[i] = (sav_hash[i-1]-id (a[i-lenb])*x[lenb-1])*hnm+ id(a[i]);
    }
}


int main(){
    int T = 0;
    int kase = 0;
    scanf("%d",&T);
    while(T--){
        kase++;
        init();
        scanf("%s",a+1);
        scanf("%s",b+1);
        lena = strlen(a+1);
        lenb = strlen(b+1);
        cal();
        for(int i=1;i<=lena;i++){
            //printf("%d %d\n",sav_hash[i],hashb);
            if(sav_hash[i]!=hashb){
                dp[i] = dp[i-1];
            }else{
                dp[i] = dp[i-lenb]+dp[i-1];
                dp[i] %= mod;
            }
        } 
        printf("Case #%d: %lld\n", kase, dp[lena]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/caomingpei/p/9460143.html