HDU 5763 Another Meaning (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式.......
比如"hehehehe"和"hehe",则有5种情况,"#hehe","he#he","hehe#","##","hehehehe"
首先我们KMP/哈希找出长串中所有可以作为和短串结尾匹配成功后的位置
然后可以得到方程
(不是子串结尾)
(是子串结尾)
至于原因呢,如果它不是子串结尾,那么它不能被替换,所以是
而如果它是子串结尾,它既可以不被替换,即
也可以被替换,那么替换整个短串,转移的地方就是
然后转移一下即可,建议从1开始读入字符串方便转移
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 100100
#define mod 1000000007
#define ui unsigned int
#define ll long long
using namespace std; int T,ls,lt;
int ed[N],nxt[N];
ui f[N];
char s[N],t[N];
void get_kmp()
{
int i=,j=;
nxt[]=;
while(i<=lt)
{
if(j==||t[i]==t[j])
{
i++;
j++;
nxt[i]=j;
}else{
j=nxt[j];
}
}
}
void KMP()
{
int i=,j=;
while(i<=ls)
{
if(j==||s[i]==t[j])
{
i++;
j++;
}else{
j=nxt[j];
}
if(j==lt+)
{
ed[i-]=;
j=nxt[j];
}
}
}
ui solve()
{
f[]=;
for(int i=;i<=ls;i++)
{
if(ed[i]==)
{
f[i]=(f[i-lt]+f[i-])%mod;
}else{
f[i]=f[i-];
}
}
return f[ls];
} int main()
{
//freopen("aa.in","r",stdin);
scanf("%d",&T);
for(int i=;i<=T;i++)
{
memset(nxt,,sizeof(nxt));
memset(ed,,sizeof(ed));
memset(f,,sizeof(f));
scanf("%s",s+),ls=strlen(s+);
scanf("%s",t+),lt=strlen(t+);
get_kmp();
KMP();
printf("Case #%d: %u\n",i,solve());
}
return ;
}
HDU 5763 Another Meaning (KMP/哈希+DP)的更多相关文章
- 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
HDU 5763 Another Meaning 题意:一个字串有可能在模式串出现多次,问有多少种可能出现的情况.关键是有重合的字串是不能同时计入的. 思路:先用kmp求出所有字串的位置.然后,dp. ...
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- 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 ...
- 【动态规划】【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 5782 Cycle(KMP+哈希)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个长度相等的字符串,输出两个字符的每个前缀是否循环相同. 思路: 如果连个串循环相 ...
随机推荐
- XSS Chanllenges 6-10
Stage #6 测试代码</xss> 存在过滤,并且也没有其他输入点,尝试构建" onmousemove="alert(document.domain),并查看源代码 ...
- luogu 3768 简单的数学题 (莫比乌斯反演+杜教筛)
题目大意:略 洛谷传送门 杜教筛入门题? 以下都是常规套路的变形,不再过多解释 $\sum\limits_{i=1}^{N}\sum\limits_{j=1}^{N}ijgcd(i,j)$ $\sum ...
- 学习笔记——ST表
作用: 给定一个数列 ai O(nlogn) 预处理 O(1) 查询区间最值 实现: 定义f(i,j) 为ai开始,包括ai的连续2^j个元素的最值 下面以最大值为例: f(i,j)表示max{ak} ...
- select Option(增加,删除,清空)
jQuery获取Select选择的Text和Value: $("#select_id").change(function(){//code...}); //为Select添加事件, ...
- Supervisor 从入门到放弃
前言 Supervisor是一个客户端/服务器系统,允许其用户在类UNIX操作系统上控制许多进程.(官方解释) 简单点来讲,就是一个监控脚本运行的工具,不过他可以统一化管理,laravel的队列文档上 ...
- LCA【模板】
#include <algorithm> #include <cstdio> #include <vector> #define N 10015 using nam ...
- codevs——T3657 括号序列
http://codevs.cn/problem/3657/ 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description ...
- thrift java示例
thrift java示例 使用IntelliJ IDEA作为开发工具: 增加proto文件夹,里面写上sayHello.proto syntax = "proto3"; opti ...
- codecombat之KithGard地牢19-37关代码分享
codecombat中国游戏网址:http://www.codecombat.cn/ 全部代码为javascript代码分享 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 19 ...
- jenkins下载
前置条件:需要有java环境的jdk 一.安装使用 下载地址:https://jenkins-ci.org/content/thank-you-downloading-windows-installe ...