Count the string(kmp+dp)

上证指数30年k线图_金融占星简单解读上证指数30年历史行情 阅读此文需要一定的占星基础知识,本人对于金融知识了解甚少,如有描述错误请包涵指正!影响大众事件发展趋势的都是外行星,即木星-冥王星这5颗,由于时间跨度长达30年,绘制社会行星的K线图工作量太大,所以只以时代行星对这30年行情的简单解读作为分享,在今后的短期分析中会逐渐加入社会行星,甚至在个股分析上还会加入个人行星。那即使只绘制时代行星,所展现出来的信息也无法在一张图上画出来,否则太乱难以分辨,所以... 阅读详情

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, 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

如果求出next数组以每个子串再用kmp分别求个数肯定会超时。

这个题利用next数组的性质,利用dp,可以得到递推式dp[i] = dp[Next[i]] + 1求,下面具体解释这个递推式的含义和成立原因

dp[i]表示的是以下标为i-1的字母为结尾的任意前缀子串的个数,为什么是i-1,因为字符串从0开始记录,next数组next[i]记录的字符串长度是i实际是下标从0~i-1,同理dp[i]也是存在长度为i的前缀中也就是下标从0~i-1,以i-1号字符为结尾的任意前缀个数

这个公式为什么是对的呢

首先+1就是加上目前这个前缀字符串本身,因为我们又增加了一个字符嘛,比如a b c d  dp[3]存abc中以c结尾任意前缀长度

                                                                                                                        0 1 2 3

+1的目的就是先把abc这个本身加上

然后再来看dp[Next[i]],Next[i]是0~i-1子串s中公共前后缀串的最大长度,同时也是这个子串公共前缀的下一个下标p,

所以dp[p]就代表了我们之前已经求完的,s中最长公共前缀(也是后缀)中以前缀最后一个字符为结尾的任意前缀个数(有点绕),同理他也是后缀的,那么后缀是不是和我们新加的i-1号字符相连,那么之前有多少个,再加上这个新字符是不是就有多少个新的字符了,然后再加1本身,就是以i-1号字符为结尾所有了前缀了

例如   0  1   2  3  4

          a   b  a  b

next  -1   0  0  1  2

dp      0   1  1  2  2

dp[1]=1->"a"   dp[2]=1->"ab"  dp[3]=2->"aba","a"  dp[3]=2->"abab","ab"

code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 200100;
const int MOD = 10007;
char s[MAXN];
int n;
int Next[MAXN];
int dp[MAXN];
int sum;
void getNext(){
    int i = -1,j = 0;
    Next[0] = -1;
    int len = strlen(s);
    while(j < len){
        if(i == -1 || s[i] == s[j]){
            i++,j++;
            Next[j] = i;
        }
        else
            i = Next[i];
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
      scanf("%d",&n);
      scanf("%s",s);
      getNext();
      int i;
      memset(dp,0,sizeof(dp));
      sum = 0;
      for(i = 1; i <= n; i++){
         dp[i] = dp[Next[i]] + 1;
         sum = (sum + dp[i])%MOD;
      }
      printf("%d\n",sum);
    }
    return 0;
}


学习笔记:TF卡启动荔枝派Nano(全志F1C200S) 荔枝派Nano编译Kernel报错报错内容如下编译烧录 报错内容如下 No controllers found No ethernet found. 从这些报错我真没看从出啥问题,也是在网上找,说是没有找到flash,是因为淘宝买的板子更换了flash,需要在u-boot里面重新适配型号: 板子的型号为xt25f128,但是实际u-boot的配置里面不是需要重新更改: u-boot/arch/arm/dts/suniv-f1c100s-licheepi-nano.dts &spi0 { pin 阅读详情

相关推荐

BitLocker恢复密钥丢失?从原理到实战的完整解锁与系统重生指南

磁盘加密技术是数据安全的核心防线,它通过对存储设备上的数据进行编码,确保未经授权的用户无法访问。其原理主要基于加密算法(如AES)与密钥管理机制,在操作系统启动前或数据读写时进行实时加解密。这项技术的核心价值在于防止设备丢失或被盗后的数据泄露,尤其对于笔记本电脑和移动设备至关重要。典型的应用场景包括企业办公电脑、存有敏感信息的个人设备以及符合合规性要求的行业。当使用Windows系统自带的BitLocker加密功能时,若因硬件改动、系统更新等触发保护机制,用户可能会面临需要输入恢复密钥才能启动的困境。本文聚

weixin_34006965的博客 487

hdu3336 Count the string

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

夏天的风 1749

RandLA-Net 调通代码1:Windows 11 + Tensorflow 2.4 + S3DIS数据集【踩坑+填坑】

TF不上进啊,学学pytorch

累了就要打游戏 2480

HDU3336 Count the string

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, we can write down all the non-empty prefixes of this str...

weixin_30502157的博客 137

ios生成海报使用dom-to-image中toPng报错isTrusted:true

主要是ios中safari浏览器解析错误,引起的报错,主要引起原因toPng会调用toSvg,toSvg直接将html标签内容通过svg中foreignObject标签直接包裹,然后转base64, 通过data:image/svg+xml;解决办法:二次生成(调用两次),判断ios or 安卓,生成第一次生成,不用参数,例如:scale: 3,在第二次生成再进行清晰度操作,避免多次生成,引起性能消耗。以上种种很多案例,使用canvase进行转换的,标签丢失,直接toPng版本的,兼容性问题。

weixin_44601570的博客 2105

Dell R730安装Windows Server 2008 R2

一、RAID驱动准备  在安装Windows server2008 R2操作系统时,如果没有发现硬盘,请将下面的阵列卡驱动先解压(在下载的.exe文件,双击点下一步会自动解压),解压后把那文件夹复制到U盘中。阵列卡 PERC9 H330 - H730 - H730P - H830 Controllers for Windows Server 2008 R2二、系统安装方法方法一:光盘安装  ①请您...

MarshLin 2万+

count the string(next数组理解)

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

Ray.C.L的博客 1082

从DDH到FastANI:原核生物分类黄金标准的进化史与技术对比

本文系统梳理了原核生物分类标准从DDH到FastANI的四次技术革命,详细解析了95%相似度阈值的科学依据及应用场景。通过对比DDH、16S rRNA、ANI和FastANI的技术特点与性能指标,为微生物研究者提供基因组分类的实战指南,并展望第三代测序时代的分类新挑战。

weixin_29275257的博客 412

Count the string(KMP 前缀个数

#include<bits/stdc++.h> using namespace std; const int N=2e5+10; vector<int> nxt; vector<int> getnxt(string s) { int n = (int)s.length(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (

我们仍未知道那天所看见的花的名字。 121

Plant Simulation数字孪生实战:从零搭建生产车间模型(附SimTalk脚本示例)

本文详细介绍了如何使用Plant Simulation从零构建生产车间数字孪生模型,包括基础建模、SimTalk脚本编程、参数化设计和高级应用。通过实战示例展示动态逻辑控制、设备故障模拟等关键技术,帮助工程师快速掌握数字孪生技术在制造业中的应用。

weixin_42527160的博客 358

浅谈专项测试之弱网络测试

浅谈专项测试之弱网络测试 一.弱网络测试背景 移动端产品的使用并非完全都是在流畅的wifi环境,大部分用户主要使用4G,3G,2G等网络,另外因为移动端产品使用的场景多变,如进公交,上地铁,坐电梯,使得弱网测试显得尤为重要。考虑到各种场景的客户端展示及容错,提升产品的印象和用户体验,提高产品的健壮性,弱网测试必不可少。 二.弱网络测试建议 网下需要进行功能点测试,无网络测试,网络切换测试等,测试过程除了关注功能点的实现,还应关注用户体验等各个方面 三.弱网络测试通用功能点总结 四.弱网络测试用例实例

ITstudy001的博客 1010

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

华为od统一考试B卷【单词搜索】C++ 实现

找到它是一个小游戏,你需要在一个矩阵中找到给定的单词。假设给定单词 HELLOWORD,在矩阵中只要能找到 H->E->L->L->O->W->O->R->L->D连成的单词,就算通过。意区分英文字母大小写,并且您只能上下左右行走,不能走回头路。

MISAYAONE的博客 708

HDU 3336 Count the string kmp+dp

点击打开链接 Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3916    Accepted Submission(s): 1834 Problem Descript

代码改变世界,keep coding。 1235

NTscan扫描工具

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

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

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

hdu 3336 Count the string KMP+DP

题目链接 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

读不懂的流年 524

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

HDU 3336 Count the string (kmp+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

Follow My Heart 364

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

还我笑颜 209

[HDU 3336]Count the String[kmp][DP]

题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k = next [ j ] 表示前缀串 Sj 的范围内(可以视为较小规模的子问题), 前缀串 Sk 是最长的&amp;能够匹配两次的前缀串. 这和我们需要的答案有什么关系呢? 题目是...

weixin_33750452的博客 73

[dp][KMP][hdu3336]Count the string

Count the stringProblem 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

Freddddd 332

(HDU)3336 - Count the string 【KMP】+【DP】

DP是一门玄学。

Doge 628

【KMP+DP】Count the string

KMP算法的综合练习 DP很久没写搞了半天才明白。本题结合Next[]的意义以及动态规划考察对KMP算法的掌握。 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 ...

weixin_30546933的博客 96

Count the string 【KMP+DP】

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 st...

几许情愁的博客 194

HDU3336 Count the string(KMP+DP)

计算字符串的每个前缀在字符串里出现的次数。 一个KMP+DP,在KMP的next数组中,next[i]=j就表示在前i个字符串当中,从前1到j的j长度字符字串和i-j+1到i的j长度的字符字串相等。那么,每一个位置的字符只要继承它的next[i]位置的数量,再加一就是它当前的数量了。 #include #include #include using namespace std; cons

HOrchard 254

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 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 pre

weixin_52278699的博客 208

HDOJ3336 Count the string(kmp + dp)

题目链接:点击打开链接 给你字符串的长度以及这个字符串,问你前缀数量之和是多少。 利用kmp中next数组的性质,将该跳多少位保存到nxt数组中,dp数组保存当前子串i所含前缀数量之和,dp[i] = (dp[nxt[i]] + 1) % MOD 计算当前结尾子串i数量加上前nxt[i]个字符这一前缀的数量。 AC代码: #include "iostream" #incl

GKHack's Blog 1152

HDU 3336 Count the string 【KMP】【dp】

题目:点击打开链接 题意:给定一个字符串,求出字符串中对应各个前缀,在字符串中出现的次数和。 分析:显然用kmp,得出next数组后观察容易得到,每一个字符的next为要跳转的位置,则当每次跳转后都要给先前会跳转的字符次数加一,故可以用dp的思想,根据当前的次数和,下一个(即序号前一个)的次数等于当前位置的次数加一,将所有的次数相加就可以得到答案了。

代码中转站 238
上一篇: Seek the Name, Seek the Fame (kmp)
下一篇: Clairewd’s message HDU - 4300 (扩展KMP)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值