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

数字IC后端设计实现OCC(On-chip Clock Controller)电路介绍及时钟树综合案例 阅读详情

Count the string

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 8
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
 

题意很简单:不同长度[1...len]前缀最大重复次数和累加。mod 10007

一个自然的写法,枚举par串(结果是TLE,看来数据比较多)

#include <bits/stdc++.h>

using namespace std;

const int MAXN=200010;
char ori[MAXN],par[MAXN];
int nex[MAXN];

void preKMP(char s[])
{
    int p=0,q=-1; nex[0]=-1;
    while(s[p]){
        while(q!=-1 && s[p]!=s[q]) q=nex[q];
        nex[++p]=++q;
    }
}

int KMP(char a[],char b[])
{
    int p=0,q=0,l=strlen(a),ans=0;
    preKMP(a);
    while(b[p]){
        while(q!=-1 && a[q]!=b[p]) q=nex[q];
        ++p; ++q;
        if(q==l)
        {
            ans++;
            q=nex[q];
        }
    }
    return ans;
}

int main()
{
    int t,len;
    for(scanf("%d",&t);t;t--)
    {
        scanf("%d",&len);
        scanf("%s",ori);
        int res=0;
        for(int i=0;i<len;i++)
        {
            par[i]=ori[i];
            par[i+1]='\0';
            res+=KMP(par,ori);
            res%=10007;
        }
        printf("%d\n",res);
    }
    return 0;
}


根据next数组的性质,next[i]存放的是第i位可以移动的最大匹配。(这个值肯定是小于i的)

就如同推出next数组一样,从1开始,设dp[i]表示以str[i]结尾的字符串含有的前缀数量,

 dp[i] = dp[next[i]] + 1,即长度小于i的前缀数量+长度为i的前缀(1)。

为什么是+1不是加别的,因为如果还存在一个重复子串,在next[i]位置到i位置之间,那么next[i]还会更大(矛盾)。

next数组的性质确保它是首尾端子串的最大匹配,所以后面不存在重复子串,除了长度为i的串本事。


回顾一下KMP和DP:

kmp思想:对字符串进行预处理,记录与当前位置i后缀相同的“最近”位置,用next[i]记录,
             保证 s[1 .. i] 中 s[i - next[i] + 1 .. i] 与 s[1 .. next[i]] 是相同的,
          以便在某处字符不匹配时,不用重新从头判断一遍,只要从对应的next[i]即可,
          因为中间有部分与自身重叠,减少了不必要的判断,实现见代码。
   
          例1:          
                  i :     1234567
                字符串 :    abcabab 
                  next[i] :     0001212
          例2:
                  i  :    12345678
               字符串 :    abababab 
                  next[i] :    00123456

          例3:
                  i :     123456789
               字符串:     ababcdabd 
                  next[i]:     001200120
    DP: 
         dp[]数组:以 i 结尾的串中所有前缀的计数和
            状态转移方程:  dp[i] = dp[next[i]] + 1;
      
              i :     123456
            字符串:    ababab   
            dp[i]:    112233

          比如当i等于5时,ababa中后面的aba与前面的aba重叠,  

      以第三个a为结尾的前缀总数对应于前面的aba的前缀数+1(ababa)


#include <bits/stdc++.h>

using namespace std;

const int MAXN=200010;
char ori[MAXN];
int nex[MAXN],dp[MAXN];

void preKMP(char s[])
{
    int p=0,q=-1; nex[0]=-1;
    while(s[p]){
        while(q!=-1 && s[p]!=s[q]) q=nex[q];
        nex[++p]=++q;
    }
}

int main()
{
    int t,len;
    for(scanf("%d",&t);t;t--)
    {
        scanf("%d",&len);
        scanf("%s",ori);
        int ans=0;
        preKMP(ori);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=len;i++)
        {
            dp[i]=dp[nex[i]]+1;
            ans+=dp[i]%10007;
        }
        printf("%d\n",ans%10007);
    }
    return 0;
}



安卓开发工程师在车载项目中的角色与技术实践 本文全面探讨了安卓开发工程师在车载项目中的关键角色,重点分析了技术栈(Kotlin/Java/Gradle)、岗位职责和任职要求。文章详细解读了车载安卓开发的核心技能,包括架构设计、性能优化和缺陷修复,并提供了20个针对性面试问题及答案。特别强调了车载环境的特殊需求,如硬件集成、安全认证和实时性要求。通过技术深度与实践案例的结合,为开发者和企业提供了从需求分析到系统优化的完整指导,旨在提升车载安卓开发的专业水平和招聘效率。 阅读详情

相关推荐

独立游戏开发者的新武器:实测腾讯混元3D自动绑骨,搭配Mixamo让角色动画制作效率翻倍

本文实测腾讯混元3D自动绑骨功能与Mixamo动画库的协同使用,显著提升独立游戏开发者的角色动画制作效率。通过解决卡通角色绑骨偏差、优化骨骼权重及动画状态机配置,使原本两周的流程缩短至三天,并分享Unity中的性能优化与版权合规要点。

weixin_42662605的博客 492

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

博客系统 - 系统简介与首页设计

用Bootstrap实现手机端的博客导航栏效果。

m0_53450409的博客 1237

[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

科普:Frenet坐标

理想情况下,描述这种常见驾驶行为在数学上应该很容易。一种方法是使用新的坐标系。现在,而不是放下一个正常的笛卡尔网格,我们做一些事情,就像你看到下面…Frenet Coordinates是一种以比传统x,y笛卡尔坐标更直观的方式表示道路位置的方式。如果我们想要方程式来描述这个动作,那就不容易了!想象一下像下面这样一条弯曲的道路,笛卡尔坐标系就位于它的上面…使用这些笛卡尔坐标,我们可以尝试描述车辆通常在路上行驶的路径…那么在Frenet坐标系下呈现的典型轨迹是什么样的?在这里,我们定义了一个新的坐标系统。

OceanStar的博客 1万+

语义通信实战:跳过“比特”保“语义”,手把手构建轻量级图像压缩重建网络(基于PyTorch)

本文详细介绍了基于PyTorch的轻量级语义通信系统LICRnet的实现,专注于图像压缩重建中的语义信息保留。通过深度可分离卷积、可变窗口多尺度注意力等创新技术,显著降低计算量并提升性能,特别适用于无人机巡检、远程医疗等物联网场景。文章包含完整的代码实现、训练策略及部署优化方案。

weixin_27697385的博客 221

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

【WiFi软件开发】IOCTL和Netlink----用户空间和内核空间交互的两种方式

linux开发中通常会涉及用户空间和内核模块的交互。本文介绍了两种典型的用户空间和内核模块通信方式:ioctl和netlink

liuwe1ye的博客 2348

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

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

weixin_33750452的博客 73

使用Simulink来模拟多旋翼无人机在风扰动条件下的飞行情况,并分析其稳定性

本教程详细介绍了使用Simulink进行多旋翼无人机风扰动稳定性分析的完整流程。首先构建包含飞行器动力学模型、控制系统和环境模拟模块的系统框架,重点讲解简化动力学方程的实现方法。随后指导设计PID控制器来维持姿态稳定,并添加随机风扰动模型模拟环境干扰。通过设置输入信号和输出显示,最终实现对不同风速条件下无人机控制性能的评估。该实例为无人机控制系统设计提供了实用的仿真方法,强调利用MATLAB工具进行可视化分析和参数优化,以提高系统抗干扰能力和稳定性。

amy_mhd的博客 421

110、3A 端到端调试:从 ISP register dump 到主观画质的完整调试流程

文章摘要 本文分享了ISP 3A(AE/AWB/AF)端到端调试的实战经验,通过具体案例解析常见问题排查方法。从硬件链路确认、寄存器dump分析、算法闭环验证到主观画质调优,提出了一套系统化调试流程。重点包括:如何通过示波器和寄存器抓包定位时序问题,AWB调试中black level校正的关键作用,AF闭环控制中hall传感器反馈的重要性,以及算法与硬件联调的"三板斧"方法论。作者强调3A调试本质是系统工程,需结合物理层信号测量、数据通路验证和算法状态跟踪,避免陷入局部优化陷阱。

sundong_81的博客 57

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

FPGA电路设计1——芯片原理图和封装制作流程

本文详细介绍了FPGA电路板设计的学习过程,以Xilinx Kintex-7系列FPGA为例。主要内容包括:1)从官网下载芯片引脚分配文件的方法;2)原理图库绘制流程,重点讲解Bank划分(配置Bank、HR/HP IO Bank、高速收发器Bank)及利用Excel预处理数据;3)使用Altium Designer的Symbol Wizard工具实现引脚自动化导入;4)原理图设计要点,包括电源系统设计(多电压需求与上电时序)、配置电路(JTAG/Flash接口)和时钟设计。

在读研究生,FPGA电路设计与功能开发。 1384

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

精雕软件(JDpaint) 5.5 绿色版

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

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

Statistics and Machine Learning Toolbox

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

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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)

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

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)

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

Gatevin的专栏 925

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

题意: 每个前缀出现次数的和. 思路: 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

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)

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

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)2203 - 亲和串【KMP】
下一篇: 杭电校第十七届程序设计竞赛暨2017省赛集训队选拔赛
ACDoge
博客等级 码龄10年 8粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值