hdu 5763 Another Meaning 哈希+dp

Another Meaning

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

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

题意:给你一个主串一个子串,然后主串中匹配到子串就可以把匹配部分改为*(也可以不改),问主串有多少钟不同的样子;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef  long long  ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e5+10;

int la,lb;
ll dp[N];
char a[N],b[N];
ull ha[N],hb;
ull seed=13331;

void init(){
   hb=0;
   for(int i=0;i<lb;i++)
       hb=hb*seed+b[i];
   ull base=1;
   for(int i=1;i<=lb-1;i++) base*=seed;
   ha[0]=a[0];
   for(int i=1;i<=lb-1;i++)
       ha[i]=ha[i-1]*seed+a[i];
   for(int i=lb;i<la;i++)
       ha[i]=(ha[i-1]-a[i-1-(lb-1)]*base)*seed+a[i];
}

int main()
{
    int cas,kk=0;
    scanf("%d",&cas);
    while(cas--){
        scanf("%s",a);
        scanf("%s",b);
        la=strlen(a);lb=strlen(b);
        if(la<lb) {printf("Case #%d: 1\n",++kk);CT;}
        init();
        for(int i=0;i<=lb-1;i++) dp[i]=1;
        if(ha[lb-1]==hb) dp[lb-1]=2;
        for(int i=lb;i<la;i++){
           dp[i]=dp[i-1]%mod;
           if(ha[i]==hb) dp[i]=(dp[i]+dp[i-lb])%mod;
        }
        printf("Case #%d: %lld\n",++kk,dp[la-1]%mod);
    }
    return 0;
}

  分析:错误点:

1.BKDRhash不太熟练,只会原来的最后输出&的形式,导致最后计算复杂;

改进:BKDRhash的形式:pre*seed+a[i],seed为13331之类的大素数,pre为i以前的哈希值;

a[i]就是字符

2,没有想到dp,当前下标i的话,dp[i]的数值,如果当前i并未匹配到子串,dp[i]=dp[i-1];

如果匹配到子串,dp[i]=dp[i-1]+dp[i-lb];

时间: 2024-10-30 18:16:43

hdu 5763 Another Meaning 哈希+dp的相关文章

HDU 5763 Another Meaning

HDU 5763 Another Meaning 题意:一个字串有可能在模式串出现多次,问有多少种可能出现的情况.关键是有重合的字串是不能同时计入的. 思路:先用kmp求出所有字串的位置.然后,dp. 二维的时候:dp[i][j] i表示前i个子串,j的值1表示一定用这个串,0表示不用.值表示字串出现的情况数. 一维的时候可以直接用dp[i] 表示前i个字串能出现的情况. 然后,状态转移就都是分为三种情况: 1)当前子串和前一个子串不冲突,dp[i] = dp[i-1] * 2. 或者 dp[i

hdu 5763 Another Meaning (KMP/哈希+DP)

题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和"hehe",则有5种情况,"#hehe","he#he","hehe#","##","hehehehe" 首先我们KMP/哈希找出长串中所有可以作为和短串结尾匹配成功后的位置 然后可以得到方

HDU 5763 Another Meaning(DP+KMP)

http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模式串,所以首先用KMP计算next数组,然后用动态规划,d[i]表示分析到第i个字符时有多少种意思. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio>

HDU 5763 Another Meaning dp+字符串hash

题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe   sub:hehe 那么,有**he.he**.和hehehe三种不同的意思, 考虑一下aaadaaa这种情况?sub:aa  前面的aaa有三种,后面的aaa有三种,所以一共应该是有9种情况. 可以考虑成3*3=9 如果你考虑分块去相乘的话,那么恭喜你,你GG了.因为这样写非常复杂,而且非常难判断. 可以考虑下dp,因为注意到,它每个单词只有

HDU 5763 Another Meaning(FFT)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解] 我们定义数组f为S串中T出现的最后一个字母所在的位置,那么ans[i]=ans[i-1]+f[i-1]?ans[i-lenT]:0,一遍递推即可,所以关键就在于求出f数组了,f数组可以用kmp求,由于最近练FFT,用FFT求距离卷积匹配为0的位置,就是f数组了. [代码] #include <c

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面