HDU3336 Count the string —— KMP next数组

题目链接:https://vjudge.net/problem/HDU-3336

 

Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11760    Accepted Submission(s): 5479


Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
 

 

Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
 

 

Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 

 

Sample Input
1 4 abab
 

 

Sample Output
6
 

 

Author
foreverlin@HNU
 

 

Source
 

 

Recommend
lcy

 

 

题解:

求每个前缀在字符串中出现(可重叠)的次数之和。next数组的应用。

1.求出该字符串的next数组。

2.枚举每个位置,然后用next数组对其进行回退,且每回退一次,都有一个前缀出现了一次(next数组的特性)。

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 typedef long long LL;
14 const double eps = 1e-6;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e9+7;
18 const int MAXN = 2e5+10;
19 
20 char x[MAXN], y[MAXN];
21 int Next[MAXN];
22 
23 void get_next(char x[], int m)
24 {
25     int i, j;
26     j = Next[0] = -1;
27     i = 0;
28     while(i<m)
29     {
30         while(j!=-1 && x[i]!=x[j]) j = Next[j];
31         Next[++i] = ++j;
32     }
33 }
34 
35 int main()
36 {
37     int T, n;
38     scanf("%d", &T);
39     while(T--)
40     {
41         scanf("%d%s", &n, x);
42         get_next(x, n);
43 
44         int sum = 0;
45         for(int i = 1; i<=n; i++)
46         for(int j = i; j>=1; j = Next[j])
47             sum = (sum+1)%10007;
48 
49         printf("%d\n", sum);
50     }
51 }
View Code

 

 

51Nod 1277 字符串中的最大值

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1277

 

题目来源:  Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 80  难度:5级算法题
 收藏
 关注
一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd。
给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值。
例如:S = "abababa" 所有的前缀如下:
 
"a", 长度与出现次数的乘积 1 * 4 = 4,
"ab",长度与出现次数的乘积 2 * 3 = 6,
"aba", 长度与出现次数的乘积 3 * 3 = 9,
"abab", 长度与出现次数的乘积 4 * 2 = 8,
"ababa", 长度与出现次数的乘积 5 * 2 = 10,
"ababab", 长度与出现次数的乘积 6 * 1 = 6,
"abababa", 长度与出现次数的乘积 7 * 1 = 7.
 
其中"ababa"出现了2次,二者的乘积为10,是所有前缀中最大的。
Input
输入字符串S, (1 <= L <= 100000, L为字符串的长度),S中的所有字符均为小写英文字母。
Output
输出所有前缀中字符长度与出现次数的乘积的最大值。
Input示例
abababa
Output示例
10

 

 

题解:

与上一题类似,只不过此题用next数组回退的方法会超时,因为假如字符串是“aaaaaaaaaa”,那么next每回退一次只会退一个,所以枚举每个位置然后next数组回退的方法,时间复杂度最坏可达O(n^2),所以容易超。故采用递推的方法,时间复杂度为O(n),详情请看代码。

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 typedef long long LL;
14 const double eps = 1e-6;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e9+7;
18 const int MAXN = 2e5+10;
19 
20 char x[MAXN];
21 int Next[MAXN];
22 
23 void get_next(char x[], int m)
24 {
25     int i, j;
26     j = Next[0] = -1;
27     i = 0;
28     while(i<m)
29     {
30         while(j!=-1 && x[i]!=x[j]) j = Next[j];
31         Next[++i] = ++j;
32     }
33 }
34 
35 int sum[MAXN];
36 int main()
37 {
38     while(scanf("%s", x)!=EOF)
39     {
40         int n = strlen(x);
41         get_next(x, n);
42 
43         memset(sum, 0, sizeof(sum));
44         for(int i = n; i>=1; i--)
45         {
46             sum[i]++;
47             sum[Next[i]] += sum[i];
48         }
49         LL ans = 0;
50         for(int i = 1; i<=n; i++)
51             ans = max(ans, 1LL*i*sum[i]);
52         printf("%lld\n", ans);
53     }
54 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7860677.html

2026年软考高级网络规划设计师第三版备考笔记 删除了所有已退出主流应用、行业淘汰的技术内容,如 HDLC、xDSL、WiMAX、HSRP/GLBP 等,让教材内容更聚焦当下一线网络工程实际场景,剔除无效考点。 阅读详情

相关推荐

Codesys 3.5报警功能配置保姆级教程:从变量定义到确认方式(含REF/ACK详解)

本文详细介绍了Codesys 3.5报警功能的配置流程,从变量定义到确认方式的选择,特别解析了REF和ACK两种确认方式的底层逻辑。通过实战案例和代码示例,帮助工程师快速掌握报警系统的搭建与调试技巧,提升工业自动化项目的可靠性。

weixin_29245767的博客 470

绝对超级详细的kmp算法,next数组求法,以及回溯的理解

今天学习kmp算法,看了好多大佬的文章,总算是解决了理解层面上的问题,现在和大家分享一下。 kmp算法是什么 KMP算法是一种改进的字符串匹配算法 主串 为:a b a c a a b a c a b a c 模式串 为:a b a c a b 目的就是为了找出模式串在主串中的起始位置。 a b a c a a b a c a b a c 此时起始位置是6。 我们就是为了确定这个位置 为什么要用...

Daaaaaaaaaale~~~~ 8805

基于STM32和MPU6050的手势控制小车设计与实现

MPU6050传感器:用于检测手势变化,采集加速度和陀螺仪数据。STM32微控制器:处理MPU6050传感器的数据,根据手势变化控制小车的运动。电机驱动模块:接收来自STM32的控制信号,驱动小车的电机,实现小车的前进、后退、左转和右转。小车底盘:作为整个系统的机械部分,提供移动平台。MPU6050是一款集成了3轴加速度计和3轴陀螺仪的传感器,具有高精度、低功耗和小体积等特点。它通过I2C接口与微控制器通信,可以实时采集加速度和角速度数据,广泛应用于运动检测、姿态控制和手势识别等领域。

m0_57781768的博客 2324

Next数组的实现步骤与代码,以及三个简单应用(包含KMP)

        简单介绍一下什么是next数组,毕竟网上有很多懂意思就行,本文的重点在代码解释与三个简单面试例题上。next数组就是记录str中每个位置前缀与后缀的匹配长度(不包含该位置自身)。例如:str="ababac",那么它的next数组就是[-1,0,0,1,2,3];第3个字符‘b’的前缀是‘a’,后缀也是‘a’,长度为1,则next[3]=1。最后一个字符‘c’的前缀就是‘aba’,...

从0开始的程序猿生活 4637

【图神经网络】从0到1使用PyG手把手创建异构图

异构图的准确定义如下:异构图(Heterogeneous Graphs):一个异构图GGG有一组节点V=v1,v2,...,vnV=v_1,v_2,...,v_nV=v1​,v2​,...,vn​和一组边E=e1,e2,...,emE=e_1,e_2,...,e_mE=e1​,e2​,...,em​组成,其中每个节点和每条边都对应着一种类型,用TvT_vTv​表示节点类型的集合,TeT_eTe​表示边类型的集合,一个异构图有两个映射函数,分别将每个节点映射到其对应的类型ϕv:V→Tv\phi_v:V\rig

小哲的博客 7851

Verilog SDF反标实战:从时序仿真到设计验证的完整流程解析

本文详细解析了Verilog SDF反标的完整实战流程,从时序仿真的必要性到设计验证的核心环节。文章深入拆解了SDF文件结构,并重点阐述了如何使用$sdf_annotate系统任务进行延迟反标注,将后端物理时序信息精确带回前端仿真,以实现更真实的时序验证,帮助工程师在流片前发现并修复潜在的时序违例问题。

bert9linguist的博客 711

HDU 3336 Count the string【KMP的next数组性质】

题目链接:HDU 3336 Count the string Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s , we can write down all the non-empty prefixes of this string. For example: s: "abab". T

memcpy0的博客 447

gdb对dwarf调试信息的解析和使用

gdb的对dwarf的调试信息的解析到使用的过程分析

JS072110的专栏 9582

HDU3336 Count the string (KMP,next数组)

因为初次学习KMP,所以这题开始有点不好理解,看了许多的题解觉得有点复杂,因为这里是只求数量,所以感觉可以进行简化。思路如下: 先求出next数组,增加的操作是在计算next数组的过程中监视每一个next[i]值是否为零,不为零则说明存在与前缀相同的字串,这时直接进行数量加1。当next数组计算完成,答案也就出来了。 我也不确定这个思路会不会露了什么数据,如果有的话欢迎大家批评指正,感谢。 //H...

qq_44724908的博客 206

【Proteus仿真】基于AT89C51单片机的单片机双向通信

本文介绍了基于AT89C51单片机实现双向通信的系统设计。系统采用MAX232芯片实现TTL与RS-232电平转换,通过CONN-D9F接口建立串行通信连接。甲机程序通过按键控制发送不同指令字符,同时接收乙机数据驱动数码管显示;乙机程序根据接收指令控制LED显示状态,并可发送0-10数字至甲机。文中详细说明了MAX232电平转换原理、DB9接口定义,以及完整的单片机控制程序代码,包括串口初始化、数据收发处理等关键功能实现,为嵌入式系统串口通信开发提供了实用参考方案。

XU157303764的博客 1107

37、深入解析SCTP与RTP传输协议

本文深入解析了SCTP与RTP两种重要传输协议。SCTP提供可靠的多流数据传输,支持关联管理、选择性确认和错误处理,适用于高可靠性场景;RTP则专注于实时音视频数据的低延迟传输,配合RTCP实现质量监控。文章详细介绍了两种协议的数据结构、工作流程及应用场景,并对比其特性,帮助开发者根据可靠性、实时性和多流需求选择合适的协议。

time3的博客 92

HDU 3336. Count the string (KMP-next数组)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意: 给定一个串s; 求各个前缀的出现次数之和。 分析: 用KMP求得next数组之后,将next数组中递增的一串数的最后一个加到答案中即可; 别忘了每个前缀自己也算出现了一次。 代码: #include <bits/stdc++.h> using namespace std; ty...

OI -> ACM -> AI 400

[QMT和ptrade量化交易小白入门]-125、菜场大妈加上四大现金行业指数风控,最大回撤从22%减少了一半

群里面有人建议,在菜场大妈策略的基础上,加入申万的行业指数(银行,钢铁,有色,煤炭)测算其宽度,当这四大行业其中有一个高就会清仓(用来控制回撤)。这背后的逻辑是,在存量资金博弈的市场环境中,资金总量相对固定,当银行、钢铁、有色、煤炭等大市值周期行业因基本面改善(如涨价、政策催化)或避险需求而走强时,会吸引大量资金涌入,导致其他板块(尤其是小市值股票)面临流动性虹吸。经过回测,对比上一个版本发现确实是可以降低一半的回撤,最大回撤从22.3%降到了11.4%,而且有很多时间是空仓等待。

木头大左的博客 251

[KMP-NEXT数组特性]HDU 3336 Count the string

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:给定一个字符串(1-200000),求出其所有前缀在自身中匹配成功的次数之和(模10007) 解题思路:利用next数组的特性,next[pos]在主串指针在pos位置失配时,子串指针应该调整到next[pos]与pos进行比较,这意味着0-(next[pos]-1)的字符串应该和(

Airarts_的专栏 664

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

HDU 3336 Count the string(KMP的next数组用法+DP)

Count the string Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes o...

qq_43627100的博客 282

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Hdu 3336 Count the string[KMP next数组的理解]

题意:求字串中【前缀+跟前缀相同的子串】的个数? Sample Input 1 4 abab Sample Output 6 abab:包括2个a,2个ab,1个aba,1个abab 这里要用到next值的意义: next[i]表示前i个字符所组成的字符串的最大前后缀匹配长度 举个例子: next[5]=2, 表示下标5前面那个字符串abcab的前后缀匹配

1535

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

HDU 3336 Count the string(KMP+Next数组递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:给定一个字符串,求所有前缀串在母串中匹配的数量,包含前缀串本身。 注意一个坑点,虽然没有没坑。比如aaaaaa,前缀串aa的匹配数目为4个。包括aaaaaa, aaaaaa, aaaaaa, aaaaaa 必须保证与前缀串匹配的串与之不交叉。 先求KMP的next数组,

Algobird 421

【代码超详解】HDU 3336 Count the String(KMP 的 next 数组的应用)

一、题目描述 二、算法分析说明与代码编写指导 KMP 算法 注意:本代码中所有字符串的下标都是从 1 开始的。 next 数组的意义: next[j]的值 = 模式串在位置j以前的 (j – 1) 个字符的前后缀相等的长度 + 1。 (当字符串的下标从 0 开始时,next[j]的值 = 模式串在位置j以前的 j 个字符的前后缀相等的长度) 令 i = n + 1,n,n - 1,……。i 要从...

COFACTOR 352

Count the string HDU - 3336(KMP/next数组)

题目 给出一个串A,求A串的所有前缀子串在A中出现的次数求和 思路 next数组表示的最大前后缀的长度,我们只要求出所有的next数组,对于next[i]=k说明前缀长度k的前缀子串出现一次,所以我们开一个map把每个对应长度的next前缀长度存起来,最后加上n就可以了 #include <iostream> #include <map> using names...

leekerian的博客 185

HDU 3336 Count the string(kmp next数组的性质)

Count the stringTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11337 Accepted Submission(s): 5285Problem Description It is well known that

gyr 318

HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)

Count the string Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6375Accepted Submission(s): 2947 Problem Description It is well kno...

310

HDU 3336 Count the string(KMP+稍微DP+next数组的运用)

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: "abab" The prefixes are: "a", "ab", "aba", "abab" For each

HJY 298

【HDU 3336】Count the string 【KMP next数组巧妙应用】

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: “abab

lzs_lazy 338

hdu3336Count the string(KMP的next[]数组的应用+KMP模板)

Countthe string TimeLimit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K(Java/Other) TotalSubmission(s) : 22   Accepted Submission(s) : 11 Problem Description It is well known th

咸鱼有梦想 359

HDU3336-Count the string-KMP(next数组理解)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 好吧,对next数组还是没有理解到位,感觉有很多的出发点。还需要去更深的理解。 #include #include using namespace std; string str; int n,Next[200005]; void get_next() { int i=0,j=-1;

wlxsq的专栏 700

Count the string hdu 3336(kmp next数组运用)

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: "ab...

Never__give__up的博客 359
上一篇: CodeForces - 960F Pathwalks —— 主席树(n棵线段树)
下一篇: Codeforces Round #467 (Div. 2)
alince20008
博客等级 码龄12年 5粉丝 152原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值