题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763

思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1]+dp[i-lenb],不匹配时dp[i]=dp[i-1]。匹配的判断可以用KMP。

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+3;
const int mod=1e9+7;
char a[N],b[N];
ll dp[N];
int lena,lenb,next[N];
void get_next()
{
int j = 0 ,k = -1;
next[0] = -1;
while(j < lenb)
{
if(k == -1 || b[j] == b[k])
{
j++,k++;
next[j] = k;
}
else k = next[k];
}
}
void kmp()
{
get_next();
int i = 0 ,j = 0;
memset(dp,0,sizeof(dp));
dp[0] = 1;
while(i<lena)
{
if(j == -1||a[i]==b[j])
{
i++;
dp[i] = dp[i-1];
j++;
}
else j = next[j];
if(j == lenb)
{
dp[i] = (dp[i-lenb] + dp[i]) % mod;
j = next[j];
}
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%s %s",a,b);
lena=strlen(a),lenb=strlen(b);
kmp();
printf("Case #%d: %lld\n",cas++,dp[lena]);
}
return 0;
}

HDU5763 another meaning -(KMP+DP)的更多相关文章

  1. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

  2. HDU 5763 Another Meaning (kmp + dp)

    Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...

  3. 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)

    2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...

  4. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

  5. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  6. hdu3689(kmp+dp)

    题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. 解法:dp+kmp优化.ans[i][j]表示i长度,走到了s的j位置的概率,当然这是在i之前没有出现s ...

  7. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...

  8. hdu 6068--Classic Quotation(kmp+DP)

    题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...

  9. HDU3336(KMP + dp)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. lrzsz在CentOS7的安装

    在超级用户下打一句命令: yum install lrzsz 或者,在普通用户打一句命令,需要输入超级用户密码: sudo yum install lrzsz 然后使用Xshell 5建立连接即可

  2. SVMtoy

    SVMtoy [label_matrix, instance_matrix] = libsvmread('ex8b.txt'); options = ''; % contour_level = [-1 ...

  3. JQ基础练习---图片划过变暗

    简单分享下,划过一张图片其余图片变暗,图片划过变暗的简单效果,JQ实现主要是css写法跟思路变化. <script src="http://ajax.googleapis.com/aj ...

  4. js仿京东轮播图效果

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  5. PDF 补丁丁 0.4.1 版:新增嵌入中文字库、替换文档字库的功能

    PDF 补丁丁 0.4.1 版新增了嵌入中文字库.替换文档字库的功能. 嵌入汉字字库 历史上有一批黄底黑字的 PDF 文档.这批文档都具有相同的问题:没有嵌入字库.在一些设备上阅读时显示乱码.复制文本 ...

  6. pandas进行数据分析需要的一些操作

    一.查看数据 1.查看DataFrame前xx行或后xx行a=DataFrame(data);a.head(6)表示显示前6行数据,若head()中不带参数则会显示全部数据.a.tail(6)表示显示 ...

  7. Javascript arguments详解

    今天我们来看看arguments对象及属性.arguments对象不能显式创建,arguments对象只有函数开始时才可用.函数的 arguments 对象并不是一个数组,访问单个参数的方式与访问数组 ...

  8. Js中 关于top、clientTop、scrollTop、offsetTop的用法

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  9. 远程连接Windows2008R2时服务器报Terminal Services错误的解决办法

    症状: 使用终端服务客户端连接到Windows Server2008 R2的机器时,如果客户端选项中选择了打印机(注1)时,它可能会在在系统事件日志中记录一个TerminalServices打印机错误 ...

  10. redis2.8--c/s架构流程