欢迎访问~原文出处——博客园-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. digest 词根 gest

    digest  /ˈdaɪdʒest/: to change food that you have just eaten into substances that your body can use; ...

  2. ado.net 访问excel

    1 类:OleDbConnection ... 2 connection string : Provider=Microsoft.ACE.OLEDB.12.0;Data Source={excelPa ...

  3. mysql 案例~ mysql故障恢复

    一 :遇到一个朋友的案例 分享下处理流程 二 : 现象 1 mysql无法启动,观察日志发现 InnoDB: Failing assertion: !m_fatal InnoDB: We intent ...

  4. delphi-TTcpServer与TTcpClient

    最简单的TTcpServer与TTcpClient通信实例-Delphi_海盗船长_新浪博客http://blog.sina.com.cn/s/blog_5383794d0100nt9u.html d ...

  5. 【Udacity并行计算课程笔记】- Lesson 2 The GPU Hardware and Parallel Communication Patterns

    本小节笔记大纲: 1.Communication patterns gather,scatter,stencil,transpose 2.GPU hardware & Programming ...

  6. winform程序生成条形码并且并且保存到本地文件中。

    今天公司让做一个输入数字.字母生成条形码并且可以以图片格式保存到本地.当看到这个需求时候感觉很搞笑,明明可以用文本框搞定的东西非得做个程序.哎,寄人篱下,不多说了,这就是养兵千日用兵一时. 我在网上找 ...

  7. 【BARTS计划】【Review_Week1】Google Docs 成为青少年们喜爱的聊天 app

    BARTS计划 · Review :每周阅读并点评至少一篇英文技术文章. 附原文链接 Google Docs 本是作为协作办公工具存在的,却成了学生们现代版“传纸条”的工具.认真的说,“你永远不知道用 ...

  8. Dom4j向XML中指定位置添加、删除、修改节点——(五)

    需求: 在第一本书作者后面增加描述 <描述>好书</描述>  思路:获取书下面下的所有节点(一个list集合),在list集合指定位置添加一个元素(list.add(index ...

  9. Java的IO流——(七)

    目录结构:

  10. 修改BlackLowKey皮肤样式,增加占屏比

    页面定制CSS代码 #home { margin: 0 auto; width: 100%; } #sideBar { min-height: 200px; padding: 0 5px 0 5px; ...