hdu 3336 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 阅读详情

Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2871    Accepted Submission(s): 1332


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
求前缀出现的次数的总和。
DP+kmp运用
next[i]保存与以s[i]为后缀的串匹配的s的前缀的最大长度,换言之,next[i]-1表示与以s[i]为后缀的串匹配的s的前缀中最接近s[i]的位置。
dp[i]代表以s[i]结尾的s的前缀(可以当成一个新的模式串)的前缀出现的次数总和。利用next[i]转移到匹配的位置,然后dp[i]=dp[next[i]-1]+1.
AC代码:
#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 200005
#define MOD  10007
int next[MAXN];
char s[MAXN];
int dp[MAXN];
int n;
void get_next()
{
    memset(next,0,sizeof(next));
    for(int i = 1; i <n; i++)
    {
        int temp = next[i - 1];
        while(temp && s[temp] != s[i])
            temp = next[temp - 1];
        if(s[temp] == s[i])
            next[i] = temp + 1;
        else
            next[i] = 0;
    }
}

int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        cin>>n;
        cin>>s;
        get_next();
        int sum=0;
        for(int i=0;i<n;i++)
        dp[i]=1;
        for(int i=0; i<n; i++)
            {
                if(next[i])
                dp[i]+=dp[next[i]-1];
                sum+=dp[i];
            }
        cout<<sum%MOD<<endl;
    }
    return 0;
}



AMESim柱塞泵建模全指南:从HCD库搭建到联合仿真 液压系统仿真中,轴向柱塞泵的建模始终是难点与热点。柱塞泵结构紧凑、功率密度高,在工程机械与航空航天领域应用广泛,然而其运动学与流体细节深度耦合,给仿真带来不小挑战。AMESim平台中的HCD库提供了一种从物理结构出发的建模思路,将柱塞泵分解为容积腔、节流口与机械约束等基础单元,从而真实刻画柱塞腔压力波动、配流特性和容积效率等关键行为,摆脱黑盒模型的局限。基于机理建模的仿真结果,可用于泵参数优化、故障特征分析以及控制策略验证。进一步结合AMESim与Simulink联合仿真,可以高效开发变量泵排量控制系统,研 阅读详情

相关推荐

数学建模--十大经典常用算法

数学建模十大算法:灰色预测,蒙特卡洛,遗传算法,模拟退火法,组合算法,神经网络,动态规划,聚类算法,类比法,概率算法,Floyd算法

qq_51533426的博客 5655

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

使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第七讲)

默认是第一种键盘模式,当需要切换模式,进入到ta_event_cb函数修改模式,下面代码以数字模式举例子lv_keyboard_set_mode(kb, LV_KEYBOARD_MODE_NUMBER);在该界面中可以设置键盘文字的大小和文本类型,最下方判断键盘是否支持中文输入(中文输入,当汉字库不够大时,用户点击中文输入会少很多字)。//当LV_USE_KEYBOARD为1时启用虚拟键盘,LV_USE_ZH_KEYBOARD为1时支持中文输入。随后在系统设置界面中点击项目选项,选择显示键盘。

weixin_38828382的博客 1420

hdu 3336 Count the string(KMP)

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

不慌不忙、不急不躁 1108

L298N电机驱动模块的接线使用与代码实现

#include "iostm8s208mb.h" #define u8 uint8_t #define u16 uint16_t #define u32 uint32_t typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned long uint32_t; // 关于LED灯的宏定义 #define LED2 PI_ODR_ODR2 //输出量 #define LE

Clichong 13万+

告别CMake依赖!在Visual Studio 2022中直接配置VTK 9.4开发环境(附完整项目示例)

本文详细介绍了如何在Visual Studio 2022中直接配置VTK 9.4开发环境,无需依赖CMake。通过图文教程和完整项目示例,帮助C++开发者快速集成VTK库,简化开发流程,提升工作效率。特别适合科学可视化任务的开发者。

weixin_26804345的博客 420

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

机器学习项目实战案例数据集

本专栏整理了《机器学习项目实战案例》,内包含了各种不同的机器学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码+数据集。

CSDN 精品推荐 3647

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

点云焊缝提取

本文介绍了一个基于PCL库的点云处理流程,包含三个核心模块:MLS平滑处理通过多项式拟合修正点云位置;差异提取通过双向匹配定位源/目标点云的非重叠区域;统计过滤剔除离群噪声点。该流程采用"平滑-筛选-净化"的通用思路,将复杂算法封装为可配置接口,实现了工业级点云预处理。文章重点分析了各模块的原理、PCL接口调用及参数设计逻辑,同时指出参数调优、算法扩展等深入探索方向,展现了PCL在点云处理中的模块化优势与实用价值。

weixin_45196569的博客 260

基于STM32L4XX、HAL库的BQ27520YZFR-G4 电量监测计驱动程序设计

(可选) -------- TS (Pin 3) 连接温度传感器。(可选) -------- BAT (Pin 2) 连接电池正极。// 假设I2C1已初始化。// 每5秒更新一次。// 短暂延时确保数据准备好。支持Impedance Track™技术,提供高精度电量测量。适用于便携式设备如智能手机、平板电脑等。// 可添加初始化验证代码。// 初始化BQ27520。// 发送控制子命令。// 配置模拟滤波器。// 配置数字滤波器。// I2C1初始化函数。

zhusl6688的博客 254

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 2204

OpenClaw 在 Docker 容器中的部署实战——零配置启动

本文介绍了OpenClaw在Docker容器中的快速部署方法,对比了本地部署与容器化部署的优势。通过官方镜像和Docker Compose两种方式,5分钟即可完成部署,并详细讲解了配置管理方案(挂载配置文件和环境变量)。文章还提供了飞书集成配置方法,以及生产环境下的日志管理、资源限制、健康检查等优化建议。最后包含常见问题排查指南,帮助开发者快速实现OpenClaw的容器化部署。

刘一说的IT专栏 925

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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

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

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

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

[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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

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 另解

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

qq_45770909的博客 160

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

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

Count the string (HDU-3336)

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

菜鸡的博客 160

hdu3336Count the string

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 给出一个字符串,求它的每一个前缀在整个字符串中出现的次数和 dp[i]代表以第i位结尾的字符串中包括多少以第i位字母结尾的前缀的出现次数 那么有dp[i]=dp[nxt[i]]+1 最后累加一下就好了 别忘了取模。#include <stdio.h> #include<cstring>

IDrandom的专栏 419
上一篇: hdu 1867 A + B for you again
下一篇: poj 1961 Period
KinFungYu
博客等级 码龄14年 39粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值