hdu3336 Count the string kmp+dp
题目链接: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的更多相关文章
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- 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 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- 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 ...
- 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]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- Count the string (KMP+DP)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 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 ...
随机推荐
- MyBatis快速入门(一)
一.MyBatis背景介绍 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的 ...
- Linux系统文件的三个重要时间详解
Linux文件三个时间的查看 Linux下使用命令stat获取文件的三个时间,先看现象 各个时间的含义: Chang time:简称ctime,一个文件或目录的更改时间.在Linux中,基本上无法知道 ...
- css2.1实现圆角边框
虽然css3的border-radius实现圆角很简单,但是我还是认为css2.1中好多技术还是很值得学习的,我也是后来才知道这就是传说中的滑动门技术.脑洞大开啊 附上demo <!DOCTYP ...
- 使用TagHelper完成分页步骤
使用TagHelper完成分页步骤 转载 2016-08-23 11:37:33 1 创建一个MyPageOpion类,用来存储分页信息,比如当前页,栏目总数,页面大小,跳转地址(RouteUrl)等 ...
- [SinGuLaRiTy] 平衡树
[SinGuLaRiTy-1009] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 二叉查找树 二叉查找树是指具有下列性质的非空二叉树: ⑴ ...
- POPTEST老李推荐:互联网时代100本必读书,来自100位业界大咖推荐 2
➤NO.30<移动的力量>[推荐人]刘九如:电子工业出版社副社长兼总编辑邬贺铨:中国工程院院士.原副院长汪力成:华立集团董事局主席➤NO.31<智慧社会>[推荐人]段永朝:财讯 ...
- JAVA 发送邮件代码---发送HTML内容
依赖包:mail.jar JAR链接地址: http://pan.baidu.com/s/1o8LNl0Y 密码: ja52 package com.dava; import java.util.Pr ...
- bit ( 比特 )和 Byte(字节)的关系 以及 网速怎么算
今天来整理一下存储单位和网速的知识. 最近几天家里网不太好,所以就了解了一下网速和电脑的存储单位的关系. 一.存储单位的bit 和 Byte 1.bit(比特) bit也就是我们不一定听说过的比特,大 ...
- 为什么不能将客户端的连接请求跳转或转发到本机lo回环接口上?
一.为什么不能将本机的请求跳转/转发到回环接口上? 如上图一样,服务器对外只开放了一个80端口,但是web服务监听在了lo 接口上8080端口上,现在要实现外网通过访问服务器的80端口,来提供web服 ...
- bigdecimal更精确的浮点处理方式
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位内有效数,超过16位,double可能会出现内存 ...