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][0] = dp[i-1][1] + dp[i-1][0] = dp[i][1] = dp[i-1][1].
2) 当前子串和前一个冲突,那就不一定是只和前一个冲突,所以从i-2开始寻找直到找到不冲突的j,有 dp[i] = dp[i-1] + dp[j]. 或者 dp[i][0] = dp[i-1][1] + dp[i-1][0] , dp[i][1] = dp[j][0] + dp[j][1].
3) 当前子串没找到上述的j子串,那么dp[i] = dp[i-1] + 1. 或者 dp[i][1] = 1.
二维代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
const int mod=1000000007;
vector <int> ans; vector <int> find_substring(string pattern, string text) {
int n = pattern.size();
vector <int> nxt(n+1, 0);
for (int i=1; i<n; ++i) {
int j = i;
while(j > 0) {
j = nxt[j];
if (pattern[j] == pattern[i]) {
nxt[i+1] = j + 1;
break;
}
}
} vector <int> positions;
int m = text.size();
for (int i=0, j=0; i<m; ++i) {
if (j<n && text[i] == pattern[j]) {
j++;
}else {
while (j>0) {
j = nxt[j];
if (text[i] == pattern[j]) {
j++;
break;
}
}
}
if (j == n) {
positions.push_back(i-n+1);
}
}
return positions;
} int dp[100005][2]; // dp[i][1] 表示前i个串一定用上第i个时 maxans dp[i][0] 表示前i个串的时候不用第i个时候的ans. int main() {
int t;
//freopen("in.cpp", "r", stdin);
cin >> t;
string str1, str2;
int cas = 0;
while(t--) {
ans.clear();
cin >> str1 >> str2;
ans = find_substring(str2, str1);
int tot = ans.size();
if (tot == 0) {
printf("Case #%d: %d\n", ++cas, 1);
continue;
} dp[0][0] = 1;
dp[0][1] = 1; for (int i=1; i<tot; ++i) {
dp[i][0] = ((dp[i-1][0]%mod) + (dp[i-1][1]%mod))%mod;
bool flag = false;
for (int j=i-1; j>=0; --j) {
if (ans[i] - ans[j] >= str2.length()) {
dp[i][1] = ((dp[j][1]%mod) + (dp[j][0]%mod)) % mod;
flag = true;
break;
}
} if (flag == false) {
dp[i][1] = 1;
}
}
// if(ans[i]-ans[i-1]>=str2.length())dp[i][1]=(dp[i][1]%mod+dp[i-1][1]%mod)%mod;
printf("Case #%d: %d\n", ++cas, ((dp[tot-1][1]%mod)+(dp[tot-1][0]%mod))%mod);
}
return 0;
}
一维代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
const int mod=1000000007;
vector <int> ans; vector <int> find_substring(string pattern, string text) {
int n = pattern.size();
vector <int> nxt(n+1, 0);
for (int i=1; i<n; ++i) {
int j = i;
while(j > 0) {
j = nxt[j];
if (pattern[j] == pattern[i]) {
nxt[i+1] = j + 1;
break;
}
}
} vector <int> positions;
int m = text.size();
for (int i=0, j=0; i<m; ++i) {
if (j<n && text[i] == pattern[j]) {
j++;
}else {
while (j>0) {
j = nxt[j];
if (text[i] == pattern[j]) {
j++;
break;
}
}
}
if (j == n) {
positions.push_back(i-n+1);
}
}
return positions;
} int dp[100005]; int main() {
int t;
//freopen("in.cpp", "r", stdin);
cin >> t;
string str1, str2;
int cas = 0;
while(t--) {
ans.clear();
cin >> str1 >> str2;
ans = find_substring(str2, str1);
int tot = ans.size();
if (tot == 0) {
printf("Case #%d: %d\n", ++cas, 1);
continue;
} dp[0] = 2;
for (int i=1; i<tot; ++i) {
if (ans[i]-ans[i-1]>=str2.length()) {
dp[i] = (dp[i-1]*2)%mod;
}
else {
bool flag = true;
for (int j=i-2; j>=0; --j) {
if (ans[i] - ans[j] >= str2.length()) {
dp[i] = (dp[j] + dp[i-1])%mod;
flag = false;
break;
}
}
if (flag) dp[i] = (dp[i-1] + 1)%mod;
}
} printf("Case #%d: %d\n", ++cas, dp[tot-1]%mod);
}
return 0;
}
体验:dp无处不在..dp大法好... >_<
HDU 5763 Another Meaning的更多相关文章
- 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, ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDU 5763 Another Meaning(FFT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...
- HDU 5763 Another Meaning(DP+KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
- HDU 5763 Another Meaning dp+字符串hash || DP+KMP
题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe sub:hehe 那么,有**he.he** ...
- TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp
Another Meaning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5763 Another Meaning (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...
- HDU 5763:Another Meaning(字符串匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Problem Description As is known to ...
随机推荐
- XAF应用开发教程(一) 创建项目
XAF是DevExpress公司的快速开发框架,全称eXpress Application Framework,是企业信息系统的开发利器,快速开发效果显著,在.net框架中,笔者至今没有找到一款可以与 ...
- netbeans环境的建立
这是一个简单的实验,熟悉NetBeans的IDE环境的开发 首先下载一个NetBeans,可以在官网上下https://netbeans.org/downloads/index.html 要装NetB ...
- 堆内存指针的管理类(禁,引数(指针copy),值copy,移)
//copyp template<typename T> class pm_copyP{ public: pm_copyP(T* _p); pm_copyP(const pm_copyP& ...
- struts2 if正确标签示例
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...
- iOS8 获取通知设置状态
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSet ...
- 【转】Github 上传代码
版权声明:欢迎转载(^ω^)~不过转载请注明原文出处:http://blog.csdn.net/catglory ლ(╹◡╹ლ) 写在前面: 弄了两小时终于搞定了,把经验整理下,方便我以后上传代码XD ...
- 关于mysql的基础知识
一.数据库的简介 什么是数据库? 数据的仓库,如:在atm的实例中我们创建一个db目录称之为数据库 什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 他们 ...
- iOS——Xcode中添加第三方库
一.只有.h和.a文件的库 1.向项目中添加三方库文件 如果添加的第三方库只有.h和.a文件,直接把文件夹拖进项目下面,这时会弹出下面的提示框,一定要勾选下面选择的选项: 这里要注意,在Add to ...
- DOM加载:浏览器渲染和操作顺序(转载 学习中。。。)
DOM加载:浏览器渲染和操作顺序 1.HTML解析完毕 2.外部脚本和样式表加载完毕 3.脚本在文档内解析并执行 4.HTML DOM完全构造起来 5.图片和外部内容加载 6.网页完成加载 基于这个顺 ...
- hdu_2089 不要62
数位动态规划 数位动态规划是求解一个大区间[L, R]中间满足条件Q的所有数字的个数(或者和,或其他)的一种方法.它通过分析每一位上的数字,一般用 dp[len][digit][...] 来表 ...