HDU 5763Another Meaning

Another Meaning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 526    Accepted Submission(s): 245


Problem Description
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”.
 
之前一直觉得这个dp是乘法的形式。..其实仔细想的话  他是加法的形式....weak
 
dp[i]表示到第i个字幕的时候能得到的 种类数
 
那么dp[i]=dp[i-1]---- 不替换的时候 
dp[i]+=dp[i-m]-------替换的时候 m是有不同含义的字符串的长度。
比如
hehehehe
hehe
可以替换的点是1、3、5
注意5这个点.替换的时候 dp[5]+=dp[1]  相当于 X*   dp[1]是X的种类数  *是我现在选择替换的情况。所以在原基础上要加上dp[1]。。。
没有理由用乘法 
/* ***********************************************
Author        :guanjun
Created Time  :2016/7/28 14:29:00
File Name     :p401.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 1000000007
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;


int f[maxn];
int vis[maxn];
ll dp[maxn];

void getfail(char *p,int *f){
    int m=strlen(p);
    f[0]=0;f[1]=0;
    for(int i=1;i<m;i++){
        int j=f[i];
        while(j&&p[i]!=p[j])j=f[j];
        f[i+1]=p[i]==p[j]?j+1:0;
    }
}
ll find(char* t,char* p,int* f){
    int n=strlen(t),m=strlen(p);
    getfail(p,f);
    int j=0;
    cle(vis);
    for(int i=0;i<n;i++){
        dp[i]=1;
        while(j&&p[j]!=t[i])j=f[j];
        if(p[j]==t[i])j++;
        if(j==m){
            vis[i+1]=1;//标记末尾匹配起点
        }
    }

    dp[0]=1;
    for(int i=1;i<=n;i++){
        dp[i]=dp[i-1];
        if(vis[i]){
            dp[i]=(dp[i-m]+dp[i])%mod;
        }
    }
    return dp[n];
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    char p[maxn];
    char t[maxn];
    int T;
    cin>>T;
    for(int i=1;i<=T;i++){
        scanf("%s %s",t,p);
        printf("Case #%d: %I64d\n",i,find(t,p,f));
    }
    return 0;
}

 

posted on 2016-07-29 12:07  Beserious  阅读(480)  评论(0编辑  收藏  举报