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 ...
随机推荐
- OBS 录制视频 自己留存
1. 下载安装 软件下载地址:https://github.com/jp9000/obs-studio/releases/download/19.0.2/OBS-Studio-19.0.2-Full- ...
- TCP传输协议
TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接,四次挥手断开连接. 三次握手 是指建立一个TCP连接时,需要客户端和服务端总共发送3个包以确认连接建立成功.在so ...
- android的五个进程优先级,内存不足时被清理的顺序
Android操作系统尝试尽可能长时间的保持应用的进程,但当可用内存很低时最终要移走一部分进程.怎样确定那些程序可以运行,那些要被销毁,Android让每一个进程在一个重要级的基础上运行,重要级低的进 ...
- SpringBoot处理静态资源的两种方式
静态资源是指----> CSS.JS之类的文件 首先创建SpringBoot Web项目 添加Spring Boot Web Starter <dependency> <gro ...
- MFC创建线程示例
MFC创建线程示例 AfxBeginThread() 创建现场的方法是AfxBeginThread()函数. 在[.CPP]文件定义一个全局变量,决定什么时候退出这个线程. BOOL g_bWillE ...
- 【ARTS】01_10_左耳听风-20190114~20190120
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- mysql系列十三、mysql中replace into和duplicate key的使用区
一.创建测试表 1.创建唯一索引"b" CREATE TABLE `test2` ( `id` int(10) NOT NULL AUTO_INCREMENT, `a` varch ...
- centos环境无法安装paramiko的问题解决
yum install openssl-devel yum install pycrypto yum install python-devel 全部安装完毕后执行pip install paramik ...
- ansible的安装部署及简单应用
Ansible 是一个配置管理和应用部署工具,功能类似于目前业界的配置管理工具 Chef,Puppet,Saltstack.Ansible 是通过 Python 语言开发.Ansible 平台由 Mi ...
- zabbix安装及简单配置
Zabbix基本介绍: zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.它能监视各种网络参数,保证服务器系统的安全运营:并提供柔软的通知机制以让系统管理员快 ...