HDU5763 another meaning -(KMP+DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763
思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1]+dp[i-lenb],不匹配时dp[i]=dp[i-1]。匹配的判断可以用KMP。
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+3;
const int mod=1e9+7;
char a[N],b[N];
ll dp[N];
int lena,lenb,next[N];
void get_next()
{
int j = 0 ,k = -1;
next[0] = -1;
while(j < lenb)
{
if(k == -1 || b[j] == b[k])
{
j++,k++;
next[j] = k;
}
else k = next[k];
}
}
void kmp()
{
get_next();
int i = 0 ,j = 0;
memset(dp,0,sizeof(dp));
dp[0] = 1;
while(i<lena)
{
if(j == -1||a[i]==b[j])
{
i++;
dp[i] = dp[i-1];
j++;
}
else j = next[j];
if(j == lenb)
{
dp[i] = (dp[i-lenb] + dp[i]) % mod;
j = next[j];
}
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%s %s",a,b);
lena=strlen(a),lenb=strlen(b);
kmp();
printf("Case #%d: %lld\n",cas++,dp[lena]);
}
return 0;
}
HDU5763 another meaning -(KMP+DP)的更多相关文章
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)
2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- hdu3689(kmp+dp)
题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. 解法:dp+kmp优化.ans[i][j]表示i长度,走到了s的j位置的概率,当然这是在i之前没有出现s ...
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- hdu 6068--Classic Quotation(kmp+DP)
题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...
- HDU3336(KMP + dp)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Linux服务器下用svn创建多个项目
(1): 创建svn仓库路径 mkdir -p /opt/svn/project1 mkdir -p /opt/svn/project2 svnadm ...
- GPOR
[tengzhenzhen15@lu01 gpor]$ for ((i=0; i<=19; i++)) do ./gpor -S 0.4 X4058_300_gpor/mytask_train. ...
- Bag of mice(CodeForces 148D )
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Android: Intent实现活动之间的交互
Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...
- asp.net 错误跳转
每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...
- 学习记录008-crond和visudo
1.每隔两个小时将/etc/servers文件打包备份到.tmp下(每次名字不同) [root@kaka cc.log]# tar zcvf /server/backup/ccc.log_$(date ...
- php中文汉字截取函数
public function substrgb($in,$num) { //$num=16; $pos=0; $bytenum=0; $out=""; while($num){ ...
- OkHttp使用全解析(转)。
Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient.关于HttpURLConnection和HttpClient的选择>>官方博客尽管Go ...
- 统计sql语句执行效率
--统计sql语句执行效率SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N ...
- JobTracker启动流程源码级分析
org.apache.hadoop.mapred.JobTracker类是个独立的进程,有自己的main函数.JobTracker是在网络环境中提交及运行MR任务的核心位置. main方法主要代码有两 ...