Count the string[KMP]HDU3336
题库链接http://acm.hdu.edu.cn/showproblem.php?pid=3336
这道题是KMP的next数组的一个简单使用,首先要理解next数组的现实意义:next[i]表示模式串的前i个字符所组成的字符串的最长前缀后缀匹配长度,就比如对于字符串"abcdabe",它的next[3]=0,因为前三个字符构成的字符子串"abc"前后缀最长匹配长度为0,而next[6]=2,因为对于子串"abcdab",它的前缀后缀最大可以匹配出"ab",长度为2,对应了它的前缀后缀最大匹配长度。
对于这道题,我们从1位置开始遍历整个next数组,首先每一个前缀串自身都对答案有1的贡献,所以首先ans++,此外,当与到next[i]数值大于0的位置,说明前i个字符组成的子串有大于零的前缀后缀匹配,即前缀串在该位置重复出现了一次,所以此时ans再加1。
详见代码:
#include<bits/stdc++.h> using namespace std;
char s[];
int t,n,nex[];
int ans; void getNex(){
nex[]=-;
int k=-,j=;
while(j<n){
if(k==-||s[j]==s[k]){
++j;++k;
nex[j]=k;
}
else k=nex[k];
}
}
int main(){
scanf("%d",&t);
while(t--){
ans=;
scanf("%d%s",&n,s);
getNex();
for(int i=;i<=n;i++){
ans++;
if(nex[i]!=)
ans++;
ans%=;
}
printf("%d\n",ans);
}
return ;
}
xxmlala-Code
Count the string[KMP]HDU3336的更多相关文章
- 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 ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- 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
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- Count the string kmp
问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个 ...
- hud 3336 count the string (KMP)
这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了…… 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(aba ...
随机推荐
- python函数基础-参数-返回值-注释-01
什么是函数 函数就是有特定功能的工具 # python中有内置函数(python解释器预先封装好的)与自定义函数(用户自定义封装的)之分 为什么要用函数 # 可以减少代码冗余,增加代码复用性 # 使代 ...
- WGAN的改进点和实操
包含三部分:1.WGAN改进点 2.代码修改 3.训练心得 一.WGAN的改进部分: 判别器最后一层去掉sigmoid (相当于最后一层做了一个y = x的激活) 生成器和判别器的loss不 ...
- cachestat 安装文档
安装环境:ubuntu18.04 安装步骤 1. 打开命令行,输入以下命令 sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ...
- 用margin还是padding ?
margin是用来隔开元素与元素的间距:padding是用来隔开元素与内容的间隔. margin用于布局分开元素使元素与元素互不相干:padding用于元素与内容之间的间隔,让内容(文字)与(包裹)元 ...
- $.ajax()在IE9下的兼容性问题
最近在主导一个项目,遇到了一点问题,跟大家分享一下. 最终bug解决方案的链接地址:http://stackoverflow.com/questions/5241088/jquery-call-to- ...
- "A valid provisioning profile for this executable was not found"问题
时间:2015年8月14日 初接触iOS,这两天真机调试的时候遇到了这个问题.如图所示: 上网查后发现,解决方法大致有以下两种: 1. provisioning profile没有被找到,需要重新导入 ...
- Selenium+java - 下拉框处理
常见下拉框也分两种:一种是标准控件和非标准控件(一般为前端开发人员自己封装的下拉框),本篇文章中将重点讲解标准下拉框操作. 1.Select提供了三种选择某一项的方法 select.selectByI ...
- Mobile game forensics
My friend Carrie'd like to know "Garena 传说对决" violates any mobile risks such as insecure d ...
- python log 设置
# -*- coding: utf-8 -*- import loggingfrom logging.handlers import TimedRotatingFileHandler # 按时间处理 ...
- 201312-2ISBN号码
问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...