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 ...
随机推荐
- 关于微信小程序图片失真的解决方案
今天来说一说 关于微信小程序的图片失真问题的解决,微信小程序的image标签要设置其宽高,不然图片若宽高过大会撑开原始图片大小的区域:如下 但是宽高设置固定了会导致有些图片和规定显示图片大小的比例不一 ...
- 一步步教你使用rem适配不同屏幕的移动设备
1.先说说几个前端常用的几个单位的概论: 1.px (pixel,像素):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物理长度,需要指定精度DPI(Dots Per Inch ...
- 【2017-03-30】JS-document对象
一.获取标记对象 1.document.getElementById("id"); 根据id找,最多找到一个. 2.document.getElementsByNa ...
- java iframe 嵌套,session失效重新登录页面嵌套问题
将后台跳转改写成 PrintWriter out = response.getWriter(); out.println("<html>"); out.print ...
- 老李分享:Android性能优化之内存泄漏1
老李分享:Android性能优化之内存泄漏 前言 对于内存泄漏,我想大家在开发中肯定都遇到过,只不过内存泄漏对我们来说并不是可见的,因为它是在堆中活动,而要想检测程序中是否有内存泄漏的产生,通常我 ...
- 通过php动态传数据到highcharts
1:在平时工作中,在对数据进行展示的时候,是直接通过后台提供的接口来获取json串,用来展示.今天别人问怎么在本地演示一下请求的动态数据. 2:在本地搭建环境,我用的WampServer,下载地址:h ...
- 查看Samba用户的方法
有时我们需要查看服务器上都注册了哪些用户,这时我们就可以用下面的命令来查看了. pdbedit可以编辑samba的用户数据库,具体使用方法可以用man查看. pdbedit -L
- JavaScript基础学习(四)—Object
一.Object的基本操作 1.对象的创建 在JavaScript中,创建对象的方式有两种:构造函数和对象字面量. (1)构造函数 var person = new Object( ...
- 微信小程序省市联动
最近呢刚好做了一个省市联动的功能,今天看到有人问这个怎么做,我就把我做的放上来共享一下: 首先呢,来看看效果,点击文字'点击',弹出选择窗口,点击取消或者确定(取消.确定按钮在选择框上边,截图有些不清 ...
- Mac系统-java环境搭建_01
一.安装jdk 下载地址:http://www.oracle.com/technetwork/Java/javase/downloads/index-jsp-138363.html 1.傻瓜式安装下一 ...