hdu_5763_Another Meaning(dp)
题意:
一个文本串A,一个模式串B,如果文本串含有模式串B,那么就能组合成多种意思,如下:
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”.
题解:
多校的官方题解,我们队当时做也是这样的思路,不过我没用KMP,暴力匹配一样也才15ms,数据比较水
1001 Another Meaning
对于这个问题,显然可以进行DP:
令dp[i]表示到i结尾的字符串可以表示的不同含义数,那么考虑两种转移:
末尾不替换含义:dp[i - 1]
末尾替换含义:dp[i - |B|] (A.substr(i - |B| + 1,|B|) = B)
那么对于末尾替换含义的转移,需要快速判断BB能不能和当前位置的后缀匹配,kmp或者hash判断即可。
复杂度:O(N)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+,mod=1e9+;
char a[N],b[N];
int dp[N];
int main(){
int t,ic=;
scanf("%d",&t);
while(t--){
scanf("%s%s",a+,b+);
int lena=strlen(a+),lenb=strlen(b+);
memset(dp,,sizeof(dp)),dp[]=;
for(int i=;i<=lena;i++){
dp[i]=(dp[i]+dp[i-])%mod;
int fg=;
if(lena-i+>=lenb)for(int j=;j<=lenb;j++){
if(a[i+j-]!=b[j])break;
if(j==lenb)fg=;
}
if(fg)dp[i+lenb-]=(dp[i+lenb-]+dp[i-])%mod;
}
printf("Case #%d: %d\n",ic++,dp[lena]);
}
return ;
}
hdu_5763_Another Meaning(dp)的更多相关文章
- HDU 5763 Another Meaning dp+字符串hash || DP+KMP
题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe sub:hehe 那么,有**he.he** ...
- Maximum Subarray 解答
Question Find the contiguous subarray within an array (containing at least one number) which has the ...
- HDU 5763 Another Meaning KMP+DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp
Another Meaning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- HDU5763 another meaning -(KMP+DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1] ...
- Another Meaning (KMP + DP)
先用KMP重叠匹配求出各个匹配成功的尾串位置.然后利用DP去求,那转移方程应该是等于 上一个状态 (无法匹配新尾巴) 上一个状态 + 以本次匹配起点为结尾的状态(就是说有了新的位置) + 1 (单单一 ...
- HDU 5763 Another Meaning(DP+KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
随机推荐
- TcpListener 示例
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; class ...
- KVM guest caching modes
kvm中host和guest各自维护自己的page caches,使得内存中有两份缓存数据.host的缓存为page cache可以理解为读缓存,guest的缓存为disk write cache,可 ...
- PHP来实现文件上传
文件php <?phpif ($_FILES["file"]["error"] > 0){ echo '错误: ' . $_FILES[" ...
- 单链表,循环链表,双向链表(C++实现)
首先是单链表(带附加表头),实现类代码如下: template<class T> struct LinkNode{//链表节点 T data; LinkNode *link; LinkNo ...
- OGG强制停止进程
1.异常现象[oracle@localhost ~]$ ggsci Oracle GoldenGate Command Interpreter for OracleVersion 11.1.1.0.0 ...
- Servlet综述
Servlet 是在服务器上运行的小程序.这个词是在 Java applet 的环境中创造的.虽然后者已很少被使用,但 servlet 却发展的很好.是一般面试都会常考的知识. 由来 servlet ...
- Qml 定义 constant
对于程序中一些常量如字符串, 实数等, C++中经常用的方法, 是定义全局常量: 或者把所有意义相近的常量用一个单例类收集起来. QML是类JSON的标识性语言, 使用js 语法去操作对象. 在QML ...
- 戏说HTML5(转)
如果有非技术人员问你,HTML5是什么,你会怎么回答? 新的HTML规范... 给浏览器提供了牛逼能力,干以前不能干的事...(确切地说应该是给浏览器规定了许多新的接口标准,要求浏览器实现牛逼的功能. ...
- CentOS/RedHat rpm方式安装Apache2.2
注:所有RPM包均从网易镜像上下载 # rpm -ivh /home/apache/apr-1.3.9-5.el6_2.x86_64.rpm warning: /home/apache/apr-1.3 ...
- 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)
一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...