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 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
随机推荐
- angular.js跨域post解决方案
跨域,前端开发中经常遇到的问题,AngularJS实现跨域方式类似于Ajax,使用CORS机制. 下面阐述一下AngularJS中使用$http实现跨域请求数据. AngularJS XMLHttpR ...
- Android使用Apache的httpmime包post提交数据
/** * 启动线程向服务器post提交数据 */ public void upLoadPic(final Intent data) { ToastUtils.toastWithMessage(get ...
- angular $parse $eval parse VS eval
$parse angular中重要指令介绍($eval,$parse和$compile) Advanced Angular: $parse $parse ---------------------- ...
- mongo数据库备份与恢复
备份:mongodump -h xx --port 27017 -u user -p pass -d database -o D:\backup\database 恢复:mongorestore -h ...
- Mac下配置Maven环境变量
Mac下配置Maven环境变量 1.先到Apache官网下载maven压缩包.下载地址:http://maven.apache.org/download.cgi 2.Maven是用Java开发的,所以 ...
- 分布式版本控制系统Git-----8.fst-forward与no fast foward
当前分支合并到另一分支时,如果没有分歧解决,就会直接移动文件指针.这个过程叫做fastforward. 举例来说,开发一直在master分支进行,但忽然有一个新的想法,于是新建了一个develop的分 ...
- 一个简单的jQuery插件开发实例
两年前写的一个简单的jQuery插件开发实例,还是可以看看的: <script type="text/javascript" src="jquery-1.7.2.m ...
- Java 1.0 类与对象
1.Java中main()的作用: a.测试真正的类 b.启动Java应用程序 2. Java程序只会让对象与对象交互 3.创建对象时存储在堆中,自动回收. 4.Java无全局变量 5.Java程序由 ...
- Java 集合 HashMap & HashSet 拾遗
Java 集合 HashMap & HashSet 拾遗 @author ixenos 摘要:HashMap内部结构分析 Java HashMap采用的是冲突链表方式 从上图容易看出,如果选择 ...
- 浙大 pat 1024题解
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...