HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - HDU3336
题意概括
给T组数据,每组数据给一个长度为n的字符串s。求字符串每个前缀出现的次数和,结果mod 10007。
题解
首先闭着眼睛KMP跑一跑。
然后我们来dp。
dp[i]表示以第i位结尾的前缀个数。
那么,根据Next的含义,不难写出dp[i]=dp[Next[i]]+1的转移方程式。
然后就OK了。
代码
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=200005,mod=10007;
int T,n,Next[N],dp[N];
char str[N];
int main(){
scanf("%d",&T);
while (T--){
scanf("%d%s",&n,str+1);
memset(Next,0,sizeof Next);
int k=0;
for (int i=2;i<=n;i++){
while (k&&str[i]!=str[k+1])
k=Next[k];
if (str[i]==str[k+1])
k++;
Next[i]=k;
}
memset(dp,0,sizeof dp);
int ans=0;
for (int i=1;i<=n;i++){
dp[i]=dp[Next[i]]+1;
ans=(ans+dp[i])%mod;
}
printf("%d\n",ans);
}
return 0;
}
HDU3336 Count the string KMP 动态规划的更多相关文章
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string(kmp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 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 ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
随机推荐
- Centos 6.5 安装Python 3.7
文档下载地址: https://files.cnblogs.com/files/flashBoxer/Centos6.5%E5%AE%89%E8%A3%85Python3.7.xml
- Informatic学习总结_day02_增量抽取
SELECT EMP.EMPNO, EMP.ENAME, EMP.JOB, EMP.MGR, EMP.HIREDATE, EMP.SAL, EMP.COMM, EMP.DEPTNO FROM EMP ...
- python3之协程
1.协程的概念 协程,又称微线程,纤程.英文名Coroutine. 线程是系统级别的它们由操作系统调度,而协程则是程序级别的由程序根据需要自己调度.在一个线程中会有很多函数,我们把这些函数称为子程序, ...
- UML和模式应用5:细化阶段(2)--细化阶段制品之领域模型
1.前言 领域模型是OO分析中最重要和经典的模型.它阐述了领域中的重要概念: 领域模型作为设计某些软件对象的重要来源,也作为案例研究中探讨的几个制品的输入: 领域模型的范围限定于当前迭代开发的用例场景 ...
- UML和模式应用4:初始阶段(2)--需求科目之进化式需求
1. 前言 UP开发包括四个阶段:初始阶段.细化阶段.构建阶段.移交阶段: UP每个阶段包括 业务建模.需求.设计等科目: 需求是UP科目之一,在初始阶段需求科目的工作量占据较大的部分.但是初始阶段的 ...
- pt-online-schema-change VS oak-online-alter-table
前言 在上篇文章中提到了MySQL 5.6 Online DDL,如果是MySQL 5.5的版本在DDL方面是要付出代价的,虽然已经有了Fast index Creation,但是在添加字段还是会锁表 ...
- 搭建RDA交叉编译器
apt-get install subversion //安装版本控制系统,便于管理文件目录 apt-get install make atp-get install gcc =======set e ...
- Visual Studio 2017中的快捷键
Ctrl+Tab: 快速切换活动文件
- 使用python命令构建最简单的web服务
可以使用python自带的包建立最简单的web服务器,使用方法: 1)切换到服务器的根目录下 2)输入命令: python -m SimpleHTTPServer 3)使用wget或者在浏览器访问测试 ...
- route 的标志位
linux下利用route命令查看当前路由信息时,会打印如下信息: root@root:/# route Kernel IP routing tableDestination Gateway ...