ACM hdu 3336 Count the string
【题意概述】
给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和。
【题目分析】
利用kmp算法的next数组 再加上dp
【存在疑惑】
在分析next数组和dp之间的关系,结论是 dp[i] = (dp[next[i]]+1); 搞不懂之间存在的联系
【AC】
#include <bits/stdc++.h>
int n,m,dp[],next[];
char s[];
void getnext() {
int i=,j=-;
next[]=-;
while(i<m) {
if(j==-||s[i]==s[j]) {
++i;++j;
next[i]=j;
} else
j=next[j];
}
}
int main() {
scanf("%d",&n);
while(n--)
{
int sum=;
scanf("%d",&m);
scanf("%s",s);
getnext();
memset(dp,,sizeof(dp));
for(int i=;i<=m;++i) {
dp[i]=(dp[next[i]]+)%;//不理解
sum=(sum+dp[i])%;
}
printf("%d\n",sum);
}
return ;
}
参考博客:https://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html
ACM hdu 3336 Count the string的更多相关文章
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string -KMP&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 3336——Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
随机推荐
- Spark:scala集合转化为DS/DF
scala集合转化为DS/DF case class TestPerson(name: String, age: Long, salary: Double) val tom = TestPerson( ...
- Hibernate(八):基于外键映射的1-1关联关系
背景: 一个部门只有一个一把手,这在程序开发中就会设计数据映射应该设置为一对一关联. 在hibernate代码开发中,实现这个业务有两种方案: 1)基于外键映射的1-1关联: 2)基于主键映射的1-1 ...
- POJ-1135 Domino Effect---最短路Dijk
题目链接: https://vjudge.net/problem/POJ-1135 题目大意: 有N个关键的多米诺骨牌,这些牌通过一些路径相连接,这些路径是由一排其他骨牌构成的.已知每一条路径上的骨牌 ...
- STL之map排序
描述 STL的map中存储了字符串以及对应出现的次数,请分别根据字符串顺序从小到大排序和出现次数从小到大排序. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { ...
- JS事件练习题
1.点击按钮连续弹窗5次 <div class="noe"> <div class="noe1" onClick="n()" ...
- ubuntu14.4 分辨率偏低
最近出了 14.04 LTS,就想安装上玩一玩.还是用 easybcd 从 windows硬盘安装.装完之后,显示效果不好于是做了如下处理: 1. 按下windows键,搜索 "附加驱动&q ...
- kafka知识体系-kafka设计和原理分析-kafka文件存储机制
kafka文件存储机制 topic中partition存储分布 假设实验环境中Kafka集群只有一个broker,xxx/message-folder为数据文件存储根目录,在Kafka broker中 ...
- Mysql中的常用函数:
Mysql中的常用函数: 1.字符串函数: (1).合并字符串 concat():// concat('M','y',"SQL",'5.5');== MySQL5.5//当传入的参 ...
- QueryRunner--常见方法
数据库链接池的使用,一方面解决了数据库访问过多时造成数据库承受的压力,另一方面也简化了数据查询,今天就 DBUtils包所提供的QueryRunner类(org.apache.commons.dbut ...
- ●BZOJ 3143 [Hnoi2013]游走
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3143题解: 期望dp,高斯消元 首先有这样一种贪心分配边的编号的方案:(然后我没想到,233 ...