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

从驱动编译到数据传输:RK3588与FPGA的PCIe通信实战解析 本文详细解析了RK3588与FPGA通过PCIe通信的实战过程,涵盖驱动编译、数据传输及性能优化等关键步骤。通过XDMA驱动实现高效DMA传输,适用于视频处理、数据采集等高性能场景,帮助开发者快速掌握嵌入式系统中的PCIe通信技术。 阅读详情

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;
}
保姆级教程:用Kvaser DB Editor从零创建你的第一个DBC文件(附CAN信号属性详解) 本文提供了一份详细的Kvaser DB Editor教程,指导用户从零开始创建DBC文件,并深入解析CAN信号属性的关键设置。通过步骤清晰的安装指南、信号属性配置详解和实用技巧,帮助工程师和学生快速掌握CAN总线通信的核心工具,提升工作效率。 阅读详情

相关推荐

告别功能限制:Win11家庭版无损升级专业版全攻略

本文详细介绍了如何将Win11家庭版无损升级到专业版的全过程,包括升级前的准备、两种官方升级方案(Microsoft Store购买和使用产品密钥)以及命令行升级方法。升级后可解锁远程桌面、组策略编辑器和BitLocker加密等高级功能,适合需要更多商业功能的用户。

weixin_32822843的博客 760

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

新手必看!无人机定高PID调参避坑指南:从理论到实战的完整流程

本文为无人机新手提供定高PID调参的完整指南,从传感器融合到抗风策略,详细解析多传感器融合、PID控制参数调试及环境干扰应对。通过实战案例和代码示例,帮助开发者快速掌握无人机定高技术,避免常见调参陷阱,提升飞行稳定性。

spark7igniter的博客 225

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

Java utgard连接OPC问题记录

怎么开启是个问题, 是在引入openscada的jar包里的Sever类中修改,这就犯难了, 其实没必要, 只要在自己项目下创建与openscada中Server类相同的路径,并复制Server过去改就行了, 项目会优先使用本地类,像下面这样。奇迹发生了,0x00000005解决. 注: 我的安全补丁编码跟网上查到的并不一样, 所以最初没有删,后来全都删了就好了,这个编码规则咱也不知道.安全日志中记录用户登录行为, OPC通信需要使用Windows系统用户进行登录, 如果用户名密码错误这里会有相关记录.

wangxin1949的博客 2844

linux基线检查、基线加固、安全漏扫、系统漏洞、centos7、文尾部附脚本

等保三级-CentOS Linux 7合规基线检查 风险分类:系统-等保三级-CentOS Linux 7合规基线检查 检测项说明: CentOS Linux 7合规基线检查,对标中国等保2.0第三级等级保护基本要求部分检查项目,仅供参考 检查项目 : 应对登录的用户进行身份标识和鉴别,身份标识具有唯一性,身份鉴别信息具有复杂度要求并定期更换 加固建议: 1、执行`cat /etc/shadow | awk -F: '($2 == "" ) { print $1}' `, 查看空密码账户并处理; 2、

swindy博客 5633

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

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

weixin_33750452的博客 73

一文搞懂深度学习:最全神经网络介绍

本文是深度学习系列文章的第二篇,我们将深入探讨各种类型的人工神经网络,探索它们独特的特性和应用。

2402_82802238的博客 2808

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

金融监管压力测试场景生成

压力测试是一种评估金融资产或机构在极端市场条件下的表现和风险承受能力的方法。它通过模拟各种不利的市场情景,测试金融机构在这些情景下的财务状况和运营能力。压力测试的作用在于帮助金融机构识别潜在风险,评估应对策略,并制定应急预案。总结压力测试场景生成系统的开发过程和实现效果,强调其在金融监管中的重要性和应用价值。随着金融市场的发展,实时压力测试将越来越重要。未来,压力测试需要更加注重实时性和动态性,及时反映市场的变化。本文详细介绍了金融监管压力测试的重要性、核心概念、算法原理、系统架构以及实战案例。

AI天才研究院 1214

虚拟化 之三 详解 ARM 平台 jailhouse 的移植适配、构建及使用(飞腾派)

本文将探索学习通过在飞腾官方提供的 Buildroot 构建系统移植适配 openEuler 内核以及 jailhouse 源码 Package 进而直接构建一个适用于飞腾派开发板的带有 jailhouse 的 openEuler 发行版。

技术干货 1016

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

python中有趣的库 freegames 的详细使用

freegames 是一个包含多个简单 Python 游戏的开源库,适合初学者学习 Python 编程和游戏开发。以下是 freegames 的详细介绍和用法示例。

数据知道的博客 3万+

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

DP是一门玄学。

Doge 628

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

IEM模型,地表粗糙度模拟

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

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

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

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

小衣同学的博客 311

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 492

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、付费专栏及课程。

余额充值