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

统计字符流中一个字符串出现的次数- KMP算法 这是google onsite的一个题目, 说是一个无限的0/1字符串流, 给定一个字符串如"110010", 求到当前为止, 在字符流中出现了多少次这种字符. 一开始想到dp来做, 然后画了一下转移方程感觉不对, 然后又感觉越看越像是KMP, 仔细分析可以发现, 其思路就是求当前字符流的后缀与给定的字符串相等即可. 按照正常的思路即取与给定字符串长度相等的字符串, 比较是否相等即可, 这种方式 阅读详情

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

居然可以用dp
dp[i]=dp[next[i]]+1 ;

#include<bits/stdc++.h>
using namespace std ;
typedef long long LL ;

const int MAXN = 200000+10 ;
const int MAXM = 1e5 ;
const int mod  =10007 ;

int m;
char str[MAXN];
int dp[MAXN],nexts[MAXN];
void getn(){
    int i,j;i=j=0;
    nexts[0]=-1;j=-1;
    while(i<m){
        if(j==-1||str[i]==str[j]) nexts[++i]=++j;
        else j=nexts[j];
    }
}

int main(){
    int t;scanf("%d",&t);
    while(t--){
        int n;scanf("%d",&n);
        scanf("%s",str);
        m=strlen(str);
        getn();
        int ans=0;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++){
            dp[i]=dp[nexts[i]]+1;
            ans=(ans+dp[i]%mod)%mod;
        }
        printf("%d\n",ans);
    }

    return  0;
}
120、ISP 驱动架构解析:从 V4L2 请求到 ISP 硬件的配置下发流程 本文以实际案例解析ISP驱动架构的V4L2到硬件配置下发流程。作者通过一个因内存共享导致画面闪烁的bug案例,详细剖析了用户空间参数通过V4L2结构体传递、驱动层拆包与硬件寄存器映射、时序同步机制等关键环节。特别强调深拷贝、dirty flag增量更新、shadow register等实现要点,并给出多路并发隔离、寄存器写入失败等典型问题的排查方法。文章建议开发者深入理解参数到寄存器的转换过程,推荐通过debugfs实时监控寄存器状态,避免过度依赖自动调试工具。全文以实践为导向,对ISP驱动开发具有直接指导 阅读详情

相关推荐

从零到一:基于Matlab的二阶系统时域性能指标计算与可视化实战

本文提供了一份基于Matlab的二阶系统时域性能指标计算与可视化实战指南。通过详细的代码示例,手把手教你如何利用Matlab计算超调量、调节时间等关键指标,并绘制阶跃响应曲线进行可视化分析,帮助控制工程学习者和从业者快速掌握系统性能评估方法。

weixin_29096377的博客 274

如何用KMP字符串匹配算法求出主串中所包含模式串的总个数

如何用KMP字符串匹配算法求出主串中所包含模式串的总个数 #include<bits/stdc++.h> using namespace std; void getnext(int next[],string s,int len) { int j=0,k=-1; next[0]=-1; while(j<len){ if(k==-1||s[j...

hero_ache的博客 1180

解决Android UVCCamera stopPreview 报错 崩溃 闪退 问题

在使用libuvccamera库的UVCCamera时,经常在调用stopPreview关闭预览的时候,app直接崩溃闪退了,经过综合各种修改源码的方案,已解决这个问题,以下请参考: 首先找到android_usbfs.c,linux_usbfs.c两个原文件,在两个文件中都可以找到这段代码,按如下示例修改: 修改前: for (i = 0; i < num_urbs; i++) { if (urb == tpriv->iso_urbs[i]) { urb_idx = i + 1;

hanye2020的博客 3581

KMP,找出s字符串在T字符中的位置,或有几个

优质博客推荐 hdu1711 #include #include #define mem(a,b) memset(a,b,sizeof(a)) int a[1000005]; int b[10005]; int nex[10005]; int n,m; void KMP() { mem(nex,0); nex[0]=-1; int i=0,k=-1; whi

zxy160的博客 827

生信入门第七课:使用FastQC软件进行测序原始fastq数据质控

从实验到数据:RNA定量、文库片段长度、fastqc质控和fastp去接头

微生信 507

一文吃透 SAP IDoc 状态码:从 TEDS1 到 WE02 排障,再到 ABAP 自动化监控

在 SAP 集成场景里,IDoc 往往是最常见、也最容易被低估的一条生命线:采购订单、发票、主数据、库存移动……只要系统之间要稳定地交换结构化业务数据,IDoc 就经常登场。真正让人头疼的,并不是 IDoc 本身,而是它在链路里到底走到了哪一步、卡在了哪里、该找谁背锅。这篇文章把常用的 IDoc 状态码按出站、入站拆开讲清楚,并且把每个状态对应的真实世界故障现象、排查入口、以及可以落地的 ABAP 监控代码串起来,让你看到状态码就能想到动作。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 434

ACM-字符串-模式串匹配-KMP算法

KMP

我了个去 5306

数据结构之博弈树搜索(深度优先搜索)

数据结构之博弈树搜索(深度优先搜索)

LucianaiB的博客 1537

KMP——字符串匹配

声明 本文主要是一些代码,加一些例题,基本为模板,仔细内容讲解会给出其他博主的链接 如果想要知道一个字符串是否在另一个字符串中出现过,那么首先想到的就是暴力枚举 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string&gt...

weixin_44519109的博客 858

AI测试:手把手教你用AI创建流程图「喂饭教程」

本文介绍了如何利用next-ai-draw-io开源项目,通过AI快速生成专业流程图,提升测试工作效率。该工具支持自然语言交互,兼容多种AI模型(如OpenAI、Claude等),并集成draw.io图表引擎,实现实时编辑。

blues_C的博客 769

MiniQMT国债逆回购策略Python代码全解析

通过本文的介绍,我们不仅学习了如何在MiniQMT平台上使用Python编写国债逆回购策略,还对策略的逻辑构建、代码实现以及风险管理有了深入的了解。国债逆回购策略是一种适合初学者的低风险投资方式,希望本文能帮助你在量化交易的道路上迈出坚实的一步。

木头大左的博客 1931

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Count the string_HDU3336

文章目录[Count the string_HDU3336](http://acm.hdu.edu.cn/showproblem.php?pid=3336)题目大意解题思路代码 Count the string_HDU3336 题目大意 一共有T组字符串,每组有一个长度为n的字符串。让你求这个字符串的每一个前缀串在这个字符串中出现的次数和是多少。 解题思路 我们对这个字符串进行KMP求出next数组,我们会发现,next[i] 表示的是,公共前缀子串的长度,进而对next数组的值作为线性DP的下标进行求解就

qq_43750980的博客 302

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

hdu3336 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. F

喵呜的Blog 2205

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU3336 Count the string It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, ...

ACMerszl的博客 177

【题解】Count the string HDU - 3336 ⭐⭐⭐ 【KMP】

Count the string HDU - 3336 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 st...

Suprit's blog 896

KMP-Count the string-HDU - 3336

KMP-Count the string-HDU - 3336 题目: 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...

njuptACMcxk的博客 207

【代码超详解】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

hdu 3336(KMP算法的应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4844    Accepted Subm

T_T 742

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,而ne...

dmqocbae156792的博客 161

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): 11760Accepted Submission...

alince20008的博客 195

HDU3336 Count the string (KMP,next数组)

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

qq_44724908的博客 206

kmp -- Count the string HDU - 3336

Count the string HDU - 3336 题意: T次询问,每次给你一个数 n 和字符串 s ,n为s的长度。问你s的所有前缀要和s串匹配时要比较多少次。 思路: next求出的就是和前缀匹配的最大长度,若长度大于0代表和前缀匹配,那么个数增加1,然后缩小前缀再进行判断。 如 ababa 当我们匹配到a的时候,得到的next[5] = 3, 这时候次数增加1, 再匹配一下next[3] = 1,值也大于0那么次数还要增加, 再匹配next[1] = 0, 等于0了代表没有前缀和它相等

dajiangyou123456的博客 159

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

HDU 3336 Count the string kmp

一、内容 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: ...

Tizzi的博客 788

hdu3336 Count the string(KMP) !!!

题意:给你一个字符串s,然后让你输出s中所有前缀在s中出现次数的和。 例如 abab ,a 2 ,ab 2 ,aba 1,abab 1 ,统计和为 6。. 思路:看网上有众多思路。看kuangbin说这题可以直接求出扩展KMP的Next数组。然后直接加和就行。还有KMP + dp,还有直接利用KMP做的,主要看如何转化题意,还有对KMP Next数组的理解。 1、e-KMP 扩展KM...

一身清贫怎敢入繁华 501
上一篇: 【 poj 1961 】Period 【KMP 求所有前缀的循环节】
下一篇: 【HDU 4552】怪盗基德的挑战书 【KMP next数组的巧妙应用】
qq_37383726
博客等级 码龄10年 195粉丝 668原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值