利用dp计算方案数(hdu 5763 ( Another Meaning ) )
来源:互联网 发布:hbase数据库设计原则 编辑:程序博客网 时间:2024/06/06 14:05
dp的特点与数学归纳法近似,都是已知当前点推导下一个点。
很多时候dp 都会被用在统计个数上。
即在现有的元素中再加上新的元素后个数是如何变化的
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763
题意:T组数据,每组数据给两个字符串一个母字符串,一个模式串。模式串有两种含义。问母字符串有多少种含义。
分析:先进行一次kmp对母串中与模式串成功匹配的最后一个字符的位置进行标记。
dp[i]表示从0到i个字符总共有多少种含义,每新增加一个字符首先dp[i] = dp[i - 1],表示新加入的这个点是不被替换的
然后判断该点是否为标记到的点,如果是那么从某位置到该位置的字符串是可以替换的,则进行替换dp[i] += dp[i - m](m为模式串的长度)。因为是从零开始的,所以会存在i - m == -1的情况,表示对从0到该点的字符串进行替换,此时dp[i] += 1;此外还需对i == 0进行特殊处理
简单来说:dp[i] = dp[i-1]不替换
dp[i] += dp[i -m]
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <sstream>#include <string>#include <algorithm>#include <list>#include <map>#include <vector>#include <queue>#include <stack>#include <cmath>#include <cstdlib>using namespace std;char s[100005],t[100005];int nexta[100005];bool idx[100005];int dp[100005];int const mod = 1000000007;//定义nexta[]大小与s相同void getnexta(char s[]){ memset(nexta,0,sizeof(nexta)); int n = strlen(s); int k = -1,j = 0; nexta[0] = -1; while(j < n ) { if(k == -1 || s[k] == s[j]) { nexta[j + 1] = k + 1; j ++; k ++; } else { k = nexta[k]; } }}void kmp(char s[],char t[])//t模式串,s母串.此种为返回首次匹配的位置,不能匹配则返回-1.{ getnexta(t); int n = strlen(s),m = strlen(t); int i = 0,j = 0; while(i < n && j < m) { if(j == -1 || s[i] == t[j]) { i ++; j ++; } else { j = nexta[j]; } if(j == m)//根据题目要求改变 { idx[i - 1] = true; j = nexta[j]; //i ++; } } return;}int main(){ //freopen("in.txt","r",stdin); int T; scanf("%d",&T); for(int tt = 1; tt <= T; tt ++) { memset(idx,false,sizeof(idx)); scanf("%s%s",s,t); kmp(s,t); int n = strlen(s); int m = strlen(t); /* for(int i = 0; i < n; i ++) { cout<<idx[i]<<endl; }*/ for(int i = 0; i < n; i ++) { if(i == 0) { dp[i] = 1; if(idx[i] == true) { dp[i] = (dp[i] + 1) % mod; } } else { dp[i] = dp[i - 1]; if(idx[i] == true) { if(i - m >= 0) dp[i] = (dp[i - 1] + dp[i - m]) % mod; else { dp[i] = (dp[i - 1] + 1) % mod; } } } } printf("Case #%d: %d\n",tt,dp[n - 1]); } return 0;}
0 0
- 利用dp计算方案数(hdu 5763 ( Another Meaning ) )
- HDU 5763 Another Meaning (DP)
- HDU-5763-Another Meaning(KMP+DP)
- HDU 5763(Another Meaning kmp+dp)
- HDU 5763 Another Meaning(kmp+dp)
- DP-HDU-5763-Another Meaning
- HDU 5763 Another Meaning (DP)
- hdu 5763 Another Meaning KMP+DP(多校)
- HDU 5763Another Meaning (DP+KMP||后缀数组)
- Hdu-5763 Another Meaning(DP+KMP)
- HDU 5763 Another Meaning(KMP+DP)
- HDU-5763-Another Meaning-hash加DP
- HDU 5763 Another Meaning dp+kmp优化
- HDU 5763 Another Meaning 多校赛 (DP + KMP)
- HDU 5763 Another Meaning(KMP+DP)
- hdu 5763 Another Meaning kmp+dp
- HDU Another Meaning DP+KMP
- HDU 5763 Another Meaning
- c++跨文件变量声明
- NYOJ 353 Dungeon Master【BFS】
- 第二章.一切都是对象之基本类型【主数据类型】数组
- Android 使用 JSON 保存应用数据
- thinkphp中无法解析__PUBLIC__ 模板常量
- 利用dp计算方案数(hdu 5763 ( Another Meaning ) )
- Java产生验证码除去特定值
- LintCode之二分查找
- Oracle中表的几种类型
- 应用层常用协议
- tjut 2669
- 【英语】七月,英语像喝水般自然
- Android 5.1 修改浏览器默认的主页
- Java中加载配置文件方式二