题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336

很容易想到用kmp

这里是next数组的应用

定义dp[i]表示以s[i]结尾的前缀的总数

那么dp[i]=dp[next[i]]+1;

代码:

 #include<stdio.h>
#include<string.h>
const int MAXN=;
const int MOD=;
int dp[MAXN];
char str[MAXN];
int next[MAXN]; void getNext(char *p)
{
int j,k;
j=;
k=-;
next[]=-;
int len=strlen(p);
while(j<len)
{
if(k==-||p[j]==p[k])
{
j++;
k++;
next[j]=k;
}
else k=next[k];
}
}
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
scanf("%s",&str);
getNext(str);
dp[]=;
int ans=;
for(int i=;i<=n;i++)
{
dp[i]=dp[next[i]]+;
dp[i]%=MOD;
ans+=dp[i];
ans%=MOD;
}
printf("%d\n",ans);
}
return ;
}

hdu3336 Count the string kmp+dp的更多相关文章

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

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

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

  3. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

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

  5. HDU3336 Count the string(kmp

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

  6. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  7. Count the string (KMP+DP)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...

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

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

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

随机推荐

  1. pyqt样式表语法笔记(下)--原创

    pyqt样式表语法笔记(下) python 启动界面 QSS pyqt 一.启动界面的设置 简单点~说话的方式简单点用一张静态图片作为程序启动界面为例. 原来的语句     python    7行 ...

  2. Java 对象序列化和反序列化

         之前的文章中我们介绍过有关字节流字符流的使用,当时我们对于将一个对象输出到流中的操作,使用DataOutputStream流将该对象中的每个属性值逐个输出到流中,读出时相反.在我们看来这种行 ...

  3. Weblogic+apache多虚拟主机

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  4. hive 动态分区数设置

    当对hive分区未做设置时,报错如下: Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Error 20004]: ...

  5. List去除重复的元素

         有两种方法,一种是去重不带顺序的,一种是去重带顺序的. /* * 方法1: 无顺序 * Hastset根据hashcode判断是否重复,数据不会重复 */ public static Lis ...

  6. .NET Core 2.0及.NET Standard 2.0

    .NET Core 2.0的发布时间,.NET Core 2.0预览版及.NET Standard 2.0 Preview大概在5月中旬或下旬发布. .NET Core 2.0正式版本发布时间大约在Q ...

  7. Python实现简易端口扫描器

    在网上的一些资料的基础上自己又添了些新内容,算是Python socket编程练手吧. #coding=utf-8 import socket import time import sys impor ...

  8. AngularJS学习笔记4

    9.AngularJS  XMLHttpRequest $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据. <div ng-app="myApp" ...

  9. 在github上搭建免费的博客

    github好多年前,大家都开始玩啦,我这个菜鸟近几年才开始.github不仅可以管理项目,还可以搭建博客.技术人员,一般用的博客为博客园,CSDN多一些.看到朋友们都弄一个,我也开始弄起来,先找点资 ...

  10. 【转】JDBC学习笔记(4)——PreparedStatement的使用

    转自:http://www.cnblogs.com/ysw-go/ PreparedStatement public interface PreparedStatement extends State ...