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 ...
随机推荐
- Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two
传送门 D. Divide by three, multiply by two •题意 给你一个数 x,有以下两种操作,x 可以任选其中一种操作得到数 y 1.如果x可以被3整除,y=x/3 2.y= ...
- 神奇的 SQL 之 CASE表达式,妙用多多 !
前言 历史考试选择题:黄花岗起义第一枪谁开的? A宋教仁 B孙中山 C黄兴 D徐锡麟,考生选C. 又看第二题:黄花岗起义第二枪谁开的? 考生傻了,就选了个B. 接着看第三题:黄花岗起义中,第三枪谁开的 ...
- 初识web
人得往前走啊 所以学学web 动态网页是指在服务器端运行的,使用程序语言设计的交互式网页,它们会根据某种条件的变化,返回不同的网页内容.可以让用户和服务器交互的网站.然而动态网站并不是指具有动画功能的 ...
- 15. Java异常处理
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 【Spring】No converter found for return value of type: class java.util.ArrayList
错误信息: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for ret ...
- superset安装文档
1 安装python3.6 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.r ...
- 基于TP5.1实用案例及教程
推荐<基于TP5.1实用案例及教程>书 目录: 通用封装 Export通用封装Import通用封装配合Import通用封装的ImportBaseVerify类Files通用封装Direct ...
- Apple放大绝进行反取证
取证说穿了其实就是攻防,这本是正义与邪恶的对决,亦即执法单位与嫌疑犯两者之间的事,但现实生活中要比这复杂多了. 怎么说呢?举个例子大家便理解了.取证人员费尽心思,用尽各种手法,努力地想要自手机上提取重 ...
- python协程详解
目录 python协程详解 一.什么是协程 二.了解协程的过程 1.yield工作原理 2.预激协程的装饰器 3.终止协程和异常处理 4.让协程返回值 5.yield from的使用 6.yield ...
- 有趣的Flex布局
对于刚接触前端的小白,在还原页面样式的时候,往往会遇到页面布局(layout)上的问题,用着生硬的padding来固定盒子的位置,不仅代码看的沉重,还得适应各种浏览器页面,始终没有办法做到统一.接下来 ...