欢迎访问~原文出处——博客园-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 动态规划的更多相关文章

  1. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  2. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  3. HDU3336 Count the string(kmp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  4. 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 ...

  5. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. 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 ...

  8. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  9. [KMP][HDU3336][Count the string]

    题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. MyBatis第一个案例的优化,通过映射文件与接口进行绑定

    1.创建表emp CREATE DATABASE mybatis; USE mybatis; CREATE TABLE emp( id INT(11) PRIMARY KEY AUTO_INCREME ...

  2. 转载-YARN的内存和CPU配置

    Hadoop YARN同时支持内存和CPU两种资源的调度,本文介绍如何配置YARN对内存和CPU的使用. YARN作为一个资源调度器,应该考虑到集群里面每一台机子的计算资源,然后根据applicati ...

  3. rem,em

    任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: 1em=16px.那么12px=0.75em,10px=0.625em.为了简化font-size的换算,需要在css中的body选择 ...

  4. dp之免费馅饼

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. android的五个进程优先级,内存不足时被清理的顺序

    Android操作系统尝试尽可能长时间的保持应用的进程,但当可用内存很低时最终要移走一部分进程.怎样确定那些程序可以运行,那些要被销毁,Android让每一个进程在一个重要级的基础上运行,重要级低的进 ...

  6. Java探针-Java Agent技术-阿里面试题

    Java探针参考:Java探针技术在应用安全领域的新突破 最近面试阿里,面试官先是问我类加载的流程,然后问了个问题,能否在加载类的时候,对字节码进行修改 我懵逼了,答曰不知道,面试官说可以的,使用Ja ...

  7. Two Sum I & II & III & IV

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  8. 【转】C++ map的基本操作和使用

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响较小,除了那个操作节点,对其它的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  9. (常用)subprocess模块 详情官方

    subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用.另外subprocess还提供了一些管理标准流(standard str ...

  10. 中文汉字和常见英文数字等的unicode编码范围实例页面

    链接: https://www.zhangxinxu.com/study/201611/chinese-language-unicode-range.html