HDU 5763 Another Meaning (DP)

Another Meaning

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


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”.
 

Author
FZU
 

Source
2016 Multi-University Training Contest 4

题意:这题题意得多看几遍,才好理解。 类似于:只由小写字母构成的字符串A中的子串B可以被替换成*,问A可以生成多少个字符串。

解法: 对于每一个A的子串,如果其等于B,则有换成*和不换成*两种情况。设dp[i]为A串前i个字母组成的字符串可以替换生成的字符串数量,m为字符串B的长度。如果A从第(i-m+1)到第m个字母组成的子串与B相等,若将其替换,则生成的字符串数量等于A的前i-m个字母组成的字符串生成的数量;若不替换,则等于前i-1的串生成的数量,即dp[i]=dp[im]+dp[i1]。由于数据比较水,字符串匹配时strncmp之类方法就能解决,正解应该是用kmp或者hash判断。

<span style="font-size:18px;">#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
char str1[maxn],str2[maxn];
int n,sum,sum1,pos[maxn],p,dp[maxn];
const int mod=1000000007;
void pipei()
{
    int i,j;
    memset(pos,0,sizeof(pos));
    p=0;
    int len1=strlen(str1),len2=strlen(str2);
    for(i=0;i<len1-len2+1;i++)
    {
       int ans=i,j=0;
       while(j<len2 && str1[ans]==str2[j])
          ans++,j++;
        if(j==len2){
          pos[p++]=ans-1;
        }
    }
}
int main()
{
    int cas=0;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s %s",str1,str2);
        pipei();
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        int ans=0,len2=strlen(str2),len1=strlen(str1);
        for(int i=0;i<len1;i++){
           if(i==pos[ans] && p){
               ans++;
               p--;
               if(i-len2<0) dp[i]=(dp[(i-1)<0?0:(i-1)]+dp[0])%mod;
               else if(i>=len2) dp[i]=(dp[i-1]+dp[i-len2])%mod;
           }
           else{
              if(i==0) continue;
              dp[i]=(dp[i-1])%mod;
           }
        }
        printf("Case #%d: %d\n",++cas,(dp[len1-1])%mod);
    }
    return 0;
}</span>


dp[i]={dp[i1]+dp[im]dp[i1](A.substr(i­|B|+1,|B|)=B)(o
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章