hdu3336 Count the string

hdu3336(kmp)Count the string Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10970    Accepted Submission(s): 5109 Problem Description It is we 阅读详情
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



这题是KMP的变形题,主要是了解next数组的用法。

题意是,给一串字符串,问这串字符串所有的前缀总共在这个字符串中出现了几次。

已经有了next数组,next[i]=j表示最大的j使得0~j==i-j~i,因此,对于样例abab,则有

       0   1   2   3

b[]  a    b   a   b

p[]  -1  -1  0   1

对于4个前缀:

a

ab

aba

abab

设dp[i]表示子串b[0~i]共含有以b[i]为结尾的前缀的数目,则以b[i]结尾的前缀数就是自己本身加上以b[p[i]]结尾的前缀数,也就是例如i=2

则有:

a

aba这两个前缀,其中a就是b[p[i]]结尾的前缀。


附代码:

#include <stdio.h>

#define MOD 10007

char b[200005];
int dp[200005];
int p[200005];
int n;

int Pre()
{
    int s,i,j;
    dp[0]=1;
    j=-1;
    p[0]=-1;
    s=1;
    for (i=1;i<n;i++)
    {
        while(j>=0 && b[j+1]!=b[i]) j=p[j];
        if (b[j+1]==b[i]) j++;
        if (j>=0) dp[i]=(dp[j]+1)%MOD;
        else dp[i]=1;
        p[i]=j;
        s=(s+dp[i])%MOD;
    }
    return s;
}

int main()
{
    int i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        scanf("%s",b);
        printf("%d\n",Pre());
    }
    return 0;
}


[特殊字符] 智能补货(AI-driven Replenishment)完整实施方案 补货量=预测需求+安全库存−当前库存补货量 = 预测需求 + 安全库存 - 当前库存补货量=预测需求+安全库存−当前库存。:基于 LSTM/Transformer 预测未来 30/60/90 天的销量。 阅读详情

相关推荐

懒人精灵手游内存技术分析

教学声明:全套教程教学用到的游戏采用手游私服网游进行测试,不针对任何游戏。教学只做计算机技术研究,不做其他用途。

yizhihaoren123的博客 1664

Count the string(前缀出现次数

题目描述: 题意: 输出所有s中前缀在s中出现的次数。 思路: 拿到这题刚开始的时候我是没有头绪的。当然,DP问题本来就是一个玄学,再加上这题还得用到KMP算法对字符串进行处理,很难想到用DP去求解(当然也可能是小编我太渣了),我们用dp[i]表示i前面所出现的前缀重复的次数,因为KMP算法中next数组的定义,next[i]就是在i前面前缀重复的上一个位置,这个说法可能不怎么好理解,这里小编再...

nuoyanli的博客 1933

修复ASUS in WinRE教程

暑假入手了天选三,发现是win11家庭版,本来想着家庭版将就一下无所谓,结果在使用的过程中发现相较于专业版真的是不能比,于是自己就重装了系统,发现ASUS in WinRE入口不见了,折腾半天最后成功修复。

weixin_46202629的博客 1万+

count the string(next数组理解)

emmm…求前缀后缀公共子串出现了几次也就是求next数组不为0出现 了几次再加上自己的串长。(之前用了几个strlen获取串承长TLE了几次猛然发现题目给了串长…我好菜啊Orz) // ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <c...

Ray.C.L的博客 1082

宇瞳杯安防镜头设计

宇瞳杯安防镜头设计

weixin_72050316的博客 1530

扩展卡尔曼滤波 (EKF, Extended Kalman Filter) 算法详解及案例分析

扩展卡尔曼滤波是一种用于估计非线性动态系统状态的递归算法。其目标是通过结合非线性系统模型和观测数据,估计系统的最优状态。

闲人编程的博客 2396

HDU(3336)

给出一段字符串s时,我们可以写出该字符串的所有非空前缀。 例如:s: "abab" 前缀是,a, ab, aba, abab 其中 a,ab出现俩次 所以所有非空前缀的总和为=1*2+1*2+1+1=6;

m0_52262443的博客 444

【PINN高级教程】范式融合与前沿应用 第5讲:贝叶斯推断与生成式建模的深度融合

正规化流(Normalizing Flow, NF)通过一系列可逆变换把简单分布映射为复杂后验,从而获得更好的表达能力。算子学习保持映射的物理正确性。贝叶斯推断给出了自然的框架,但直接对高维函数空间做采样(如 MCMC)几乎不可行。1️⃣ 在自己的 PDE 上跑一遍完整的 Bayesian PINN 流模型。的学习机制深度融合,以在保持物理一致性的前提下,高效地从后验分布中采样。2️⃣ 将每个 θ 直接映射到 FNO 权重,得到不同解的采样。的后验 p(θ∣x,y)p(θ∣x,y) 进行近似。

u012133341的博客 477

HDU 3336 Count the string(KMP+DP)

求字符串中的每一个前缀在字符串中的出现个数 样例解释 :a 出现两次,ab 两次 ,aba 一次,abab 一次 next[i] 不为 0,说明出现过前缀,改前缀的贡献会在出现一次,所以 dp[i]=dp[next[i]]+1 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define rush() int T;c...

C_Dreamy的博客 502

如何借助Gemini 3 Pro完成一篇文献精读?(劝收藏)

金句必须是原文的完整句子,并附上该句子在文中的大致位置。你是一名资深科研文献评估专家,请根据我提供的文献信息(标题、作者、期刊、时间、引用、摘要、结论、图表)依次输出:①影响力评估:按期刊、作者、时间、引用、问题重要性给出1–10分及简述;用上述方法再结合Gemini 3 Pro的辅助,原本需要3小时才能读完一篇文献,现在只需50分钟,不管是顶刊论文还是领域内冷门论文,都可以用这套方法,另外七哥设计的高阶精读提示词,需根据自己的文献内容做调整,希望这篇文章的方法能帮你高效精读文献,掌握文献的精华内容。

智能AI让学术科研更容易 1309

【STK】手把手教你利用STK进行光电红外仿真04-STK/EOIR模块的实战操作1航天器星载相机对LEO卫星成像

今天引入一个实际的场景,帮助大家完成对于这个模块的理解与运用。

weixin_44184852的博客 2423

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

《ATF(TF-A)专题精讲》目录——146篇,完整覆盖整个ATF的官方文档

本文摘要介绍了Trusted Firmware-A (TF-A)项目的技术文档体系。主要内容包括:项目概述、组件解析(如ARM RAS/RME/CCA等安全技术)、系统设计(启动流程、中断管理、信任链等)、开发指南(构建环境、移植策略)、性能分析、平台支持(涵盖NXP、Marvell、Xilinx等主流芯片厂商)、开发流程规范以及安全通告(CVE漏洞分析)。文档体系全面覆盖了ARM安全固件的技术实现、硬件适配和开发实践,为开发者提供了从理论到实践的完整参考。

Linux内核架构师 1118

hdu 3336 Count the string(KMP)

题目链接:hdu 3336 Count the string 题目大意:给出一个字符串,要求计算所有前缀串在该字符串中出现次数的总和。 解题思路:首先求出jump数组,然后对于每个位置来说,如果jump[p]!=0的话,即为存在一个前缀串,注意这里p = jump[p], 因为前缀串中含可能包含前缀串。最后要+n。 #include #include con

不慌不忙、不急不躁 1108

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

HDU3336Count the string

HDU3336Count 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 p...

weixin_30500473的博客 75

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

hdu3336-Count the string

Count the string 使用get_next函数多了,渐渐对get_next函数的理解又深了一点点, 这其中dp[i]从1加到n dp[i]=dp[next[i]]+1; // File Name: hdu3336.cpp // Author: bo_jwolf // Created Time: 2013年04月30日 星期二 20:39:15 #include #

bo-jwolf的专栏 1235

HDU 3336 Count the string(KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 解题思路: 先把原数组构造失配数组 fail[i] 表示0~i-1最长相同前后缀 首先我们先令ans = len,也就是先把每个前缀本身加上去 我们可以保证在fail数组所得到的后缀不会与这些重合,不然那叫前缀。 接下去遍历fail[2]~fail[len] 对于每个fail我们求...

shadiao.的博客 198

[HDU 3336] Count the string

题目传送-HDU3336 题意: 给你一个长度为\(n\)的字符串\(S\),问这个字符串的所有前缀在字符串中出现的次数之和(mod \(10007\)) \(n\le 200000\) 题解: 1.考虑KMP,并记录\(f_i\)表示以\(i\)为结尾的所有串中是前缀的个数,那么答案显然是所有\(f\)之和 根据KMP中nxt数组的定义,\(f_i=f_{nxt_i}+1\)(如果这里...

A4401780916的博客 117

HDU 3336 Count the string

Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description It is well known that AekdyCoin is good at string problems as well as number th...

忘梦心的博客 272

HDU 3336 Count The String

K - Count the string                                 HDU - 3336题意:例如abab这样的字符串的子字符串看为有a,ab,aba,abab四种,就是从第一个开始,每次加一个元素,也就是所有的非空前缀,其中a,ab在原字符串中出现两次,aba,abab各出现一次,所以一共六次,结果要%10007 对,每次都是从0开始更新,很慢分析一下这个a...

STOP 682

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的博客 787

HDU 3336 Count the string 另解

嗯 看别人的方法千篇一律(都是 kmp) 想个另类方法解决 其实本质上差不多 Orz HDU 3336 是个前缀问题 并且有个规律 --------- 下一位可能的情况 只有在这一位可能的基础上才有可能 比如 abcabcaca 先把 四个a入链表 对于 ab 只可能 是 上一位是 a 的情况下才有可能 所以 把第三和第四个a 出链表 此时 链表中 还剩 2个元素 当c入队时在继续筛选 这样子快...

qq_45770909的博客 160

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 string. For example:  s: "abab...

cpx17852033609的博客 259

HDU-3336 Count the string

题意:给一个串,求出这个串所有前缀出现的次数和。 之前没考虑到,next[i]指第i个字符新加进来时的最长公共前后缀长度; 当i=6时,即加上第6个字符b时,next[6] = 4,此时前缀abab == 后缀abab(加粗位置为新加入的字符b),结果+1, 而next[4] = 2,则ab == ab,即前缀abab 与 后缀abab中的ab相等,但因为后面的b是新加进来的,所以结果+1。 #include <cstdio> #include <cstring> using na

qq_25243147的博客 238

HDU3336 Count the string

题目链接:HDU3336 Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7642    Accepted Submission(s): 3553 Problem Descri

teddywang 1029
上一篇: hdu2203 亲和串
下一篇: poj2499 Binary Tree
magicnumber
博客等级 码龄18年 119粉丝 196原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值