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

深度探索:非线性随机森林(Nonlinear Random Forest,NL-RF)原理及应用 非线性随机森林(NL-RF)旨在通过结合集成方法与高级技术来捕捉和建模数据中的非线性关系,从而改善传统随机森林在处理复杂非线性数据结构时的表现。 阅读详情

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”
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
HDOJ Monthly Contest – 2010.03.06


解题思路
dp[i]表示以s[i]前一个位置结尾可以得到的前缀串个数,因为你的s[f[i]]与s[i]时不同的,但是这个数组的的深层含义却是0-f[i]-1的这个前缀会以s[i]-1结尾,中会能匹配到这个前缀。
我们得到的f数组,,,,
所以当f[i]==0时,说明前面没有匹配项,dp[i]=1,及他本身
else dp[i]=dp[f[i]-1]+1;

代码:

#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
#define INF 0x3f3f3f3f
#define clock CLOCKS_PER_SEC
#define cle(x) memset(x,0,sizeof(x))
#define maxcle(x) memset(x,0x3f,sizeof(x))
#define mincle(x) memset(x,-1,sizeof(x))
#define maxx(x1,x2,x3) max(x1,max(x2,x3))
#define minn(x1,x2,x3) min(x1,min(x2,x3))
#define cop(a,x) memcpy(x,a,sizeof(a))
#define FROP "hdu"
#define C(a,b) next_permutation(a,b)
#define LL long long
#define smin(x,tmp) x=min(x,tmp)
#define smax(x,tmp) x=max(x,tmp)
using namespace std;
const int mod=10007,N=200005;
char s[N];
int f[N];
int dp[N];
int T,n;
void get_fail()
{
    f[0]=0,f[1]=0;
    int j=0;
    for(int i = 1; i < n; i++)
    {
        j=f[i];
        while(j && s[j]^s[i])j=f[j];
        f[i+1]=s[i]==s[j]?j+1:j;
    }
}
int main()
{
    freopen(FROP".in","r",stdin);
    freopen(FROP".out","w",stdout);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        scanf("%s",s);
        get_fail();
        dp[0]=1;
        int sum=0;
        for(int i = 1; i <= n; i++)
        {
            if(!f[i])dp[i]=1;
            else
            dp[i]=dp[f[i]-1]+1;
            sum=(sum+dp[i])%mod;
        }
        printf("%d\n",sum);
    }
    return 0;
}
CST仿真实例001:圆极化平板天线仿真 CST仿真实例001:圆极化平板天线仿真实例 阅读详情

相关推荐

Vivado 2023.1及更早版本永久License全功能解锁:HLS、System Generator等IP核使用详解

本文深入解析Vivado 2023.1及更早版本的全功能许可证,详细指导如何解锁HLS、System Generator等高级IP核。通过许可证类型对比、功能验证方法和实战应用指南,帮助FPGA开发者充分利用Vivado工具套件,提升复杂系统设计和算法加速的效率。

weixin_31286719的博客 378

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

海康视觉平台VisionMaster实战入门:从通讯配置到工具应用

本文为海康VisionMaster视觉平台实战入门指南,详细介绍了从软件安装准备、与PLC等外部设备的通讯配置,到匹配定位、测量、畸变矫正等核心工具的应用方法。通过实战案例拆解,帮助工程师快速掌握如何搭建完整的机器视觉检测与引导定位系统,实现工业自动化项目落地。

weixin_29224309的博客 1009

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

糖尿病预测(深度学习pytorch实现-最新)

本次做的项目是糖尿病的预测,也是来自kaggle上面的一个小项目。根据一个人的怀孕次数、血糖、血压、皮肤厚度、胰岛素指标、身体质量指数、糖尿病谱系指数、年龄等身体参数来预测一个人是否患有糖尿病。

NangoGreen的博客 5358

定位精度再突破!Inner-MPDIoU让YOLOv11边界框回归更精准

Inner-MPDIoU 凭借辅助边框与最小点距离的双机制创新,彻底解决了传统 IoU 损失在边界框回归上的精度瓶颈。本教程从原理到代码实现,再到工业场景适配,完整呈现了其在 YOLOv11 中的集成路径。只需简单修改两处核心脚本,你的目标检测模型就能在边界框定位精度上实现跨越式提升——无论是学术研究中的指标突破,还是工业场景下的精细检测需求,Inner-MPDIoU 都能成为你攻克边界框回归难题的利器。现在就动手尝试,让你的检测模型在定位精度上再上一个台阶吧。

FJN110的博客 158

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

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

weixin_33750452的博客 73

手把手教你完成基于matlab的路面裂缝检测系统设计

裂缝是路面病害最早的形态之一,只有在裂缝出现的早期进行维护,才能有效地提高公路的使用寿命。因此,路面裂缝的检测识别系统对于提升公路质量有着重大的意义。首先,对路面裂缝图像进行相应的预处理,目的是减小环境因素的影响,消除无用的噪点。然后提取目标图像特征,判断路面裂缝类型,标记出路面裂缝的位置。最后通过设计的 GUI 界面来集成各个关键步骤,显示所得到的试验结果。仿真结果表明,设计的路面裂缝检测识别系统是高效可行的。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1216

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

2025.06.06【Ribo-seq】|riboWaltz:P-site定位与三碱基周期性分析流程

同学们,大家好!今天我们要学习的是riboWaltz——一款专为Ribo-seq(核糖体足迹测序)数据设计的R包,主要用于P-site定位优化和三碱基周期性分析。P-site的准确定位是Ribo-seq下游所有翻译组学分析的基础,riboWaltz为我们提供了系统、可视化、易用的解决方案。P-site定位优化:自动识别不同长度RPF的最佳P-site偏移量。三碱基周期性分析:可视化RPF在CDS、UTR等区域的分布,评估数据质量。多样本/多条件支持:可同时处理多个样本,便于批量分析和对比。

1791

[QMT量化交易小白入门]-九、如何用Python记录每一笔交易和调试,报错信息?

定义一个函数来获取日志记录器。: 返回全局的日志记录器,以便在其他地方调用。

木头大左的博客 2143

HDU-3336 Count the string(KMP+DP)

加油。 To be better. 题目链接: https://cn.vjudge.net/problem/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 do...

A's World 655

AIGC 算法工程师面试八股文全解析:Diffusion Models 原理、推导与实战要点

特性 | 重参数化技巧 (Reparameterization Trick) |本质| 将随机变量的采样过程从不可导的随机操作转换为可导的确定性函数与一个已知分布的随机噪声的组合。目的。

m0_71589036的博客 403

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

DP是一门玄学。

Doge 628

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

HDU - 3336 Count the string kmp+dp

题目链接:https://cn.vjudge.net/problem/HDU-3336 题意:字符串所有前缀的出现次数 题解:dp[ i ] 表示以i为结尾i 前缀的个数 ,所以dp[i] = dp[nex[i] ] + 1 #include <bits/stdc++.h> using namespace std; const int N = 200100; const ...

还我笑颜 230

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

点击打开链接 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

hdu 3336 Count the string (kmp + dp)

http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给出一个长度为n的字符串,求出该字符串的所有前缀出现的总次数 思路:kmp求出该字符串的next数组,第i个位置dp[i]可有dp[next[i]]+1得到 #include #include #include #include #include using namespace s

大太阳 782

hdu3336 Count the string kmp+dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么dp[i]=dp[next[i]]+1; 代码: 1 #include<stdio.h> 2 #include<string.h> 3 const i...

weixin_30821731的博客 159

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 of this string. F...

PleasantlY1的博客 369

HDU 3336 Count the string 后缀数组 或 (KMP + DP)

题目大意: 就是现在给出一个长度不超过20W的字符串S, 对于这个字符串求其所有前缀在串中出现次数的和, 结果对10007取模 大致思路: 很容易想到后缀数组, 找到sa[i] = 0的那个就是串S, 那么找出这个位置向两边能扩展到的长度即可, 就是利用一下height数组就行了, 没什么难度= = 还是不清楚的话看代码细节吧 代码如下: Result  :  Acce

Gatevin的专栏 925

hdu3336 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: s: "abab...

小衣同学的博客 309

HDU 3336 Count the string kmp 递推dp

题意: 每个前缀出现次数的和. 思路: cnt[pos]记录第pos个前缀x在比他长的前缀y中出现的次数, 递推式: 定义原串的前缀为x类串. 每个长度为a的x类串s会包含2个长度为nxt[a]的x类串t, 而两个相邻的s会有一个重复的t, 所以cnt[a]个s会有cnt[a]+1个t. 最后答案加上每个前缀本身共m. 具体见代码. #include <iostream>...

swqeaaa的博客 209

hdu 3336 Count the string 【kmp + dp】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=3336题意:求给出的字符串的所有前缀出现的次数和。dp的思想还是看的别人的题解。窝就不写啦。代码:#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #inc

mfcheer 491

HDU 3336 Count the string(KMP+DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给你一个字符串,计算其所有前缀在该字符串出现的次数的总和。 思路:next[j]=i,代表s[1...i]==s[j-i+1....j],设f[i]表示以s[i]结尾的子串总共含前缀的数量,f[j]=f[i]+1,即以i结尾的子串中含前缀的数量加上前j个字符这一前缀。 #incl...

weixin_33857679的博客 85
上一篇: [NOIP模拟][bfs][dp][CSD][表达式的计算]
下一篇: [模板][数论][gcd+exgcd+sieve+power+euler_phi+euler_table+inv]
Freddddddddddd
博客等级 码龄10年 2粉丝 42原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值