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

定制PYNQ的overlay 一、用HLS生成一个定制IP(加速器)(1)首先打开vivado hls,本人的版本是2017.2,点击创建一个新工程。(2)设置工程名和路径,顶层函数设置为add,创建一个新的C++程序,名字叫做adder.cpp,不用在testbench中添加文件。选择板子的时候搜索xc7z020clg400-1,这是PYNQ的板子号,选择完成。             (3)在左侧的菜单栏里选择Sourc... 阅读详情

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;
}



Reactor 响应式编程(第一篇:Reactor核心) 响应式:响应式编程: 全异步、消息、事件回调默认还是用当前线程,生成整个流、发布流、流操作.log();//只要不指定线程池,默认发布者用的线程就是订阅者的线程; 阅读详情

相关推荐

交叉熵损失函数基本概念及公式

对于二分类问题,模型的输出通常是一个标量,表示样本属于正类的概率。因此,在二分类的交叉熵损失函数中,我们只需要考虑一个概率值,即模型预测为正类的概率。具体来说,当真实标签为1时,我们关注模型预测为正类的概率;当真实标签为0时,我们关注模型预测为负类的概率。因此,二分类的交叉熵损失函数可以表示为:其中,y 表示真实标签,取值为 0 或 1;p 表示模型预测为正类的概率。而对于多分类问题,模型的输出通常是一个向量,表示样本属于各个类别的概率。因此,在多分类的交叉熵损失函数中,我们需要考虑所有类别的概率。

qlkaicx的博客 1万+

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

从S参数矩阵到硬件实现:手把手教你用耦合微带线设计Marchand巴伦

本文详细介绍了使用耦合微带线设计Marchand巴伦的完整流程。从奇偶模理论和S参数矩阵分析入手,结合ADS仿真软件进行参数优化与电磁仿真验证,最终指导PCB布局、制作与实测调试。文章以2.4GHz Wi-Fi前端电路为例,手把手演示了从理论计算到硬件实现的全过程,并分享了关键的工程经验与调试技巧。

weixin_29271263的博客 536

[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

告别‘小目标’丢失!手把手复现DNANet红外检测网络(PyTorch版)

本文详细介绍了如何使用PyTorch从零实现DNANet红外小目标检测网络,重点解析了DNIM模块和CSAM注意力机制的设计与实现。通过实战教程,帮助开发者解决红外小目标检测中的特征提取和训练调参难题,适用于军事侦察、安防监控等场景。

weixin_28701593的博客 252

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

MacBook 本地化部署 Dify 指南

dify 怎么安装部署 有哪些坑 !

weixin_44542598的博客 1522

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

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

weixin_33750452的博客 73

禾赛XT16 vs XT32激光雷达实测对比:选16线还是32线?看完这篇不纠结

本文深度评测禾赛XT16与XT32激光雷达的性能差异与应用选择。通过实测数据对比,分析16线与32线激光雷达在点云密度、动态场景适应性及与IMU协同工作等方面的表现,帮助用户根据自动驾驶、机器人等不同应用场景做出最优选型决策。

table的专栏 945

cesium加载geotiff的 四种方法

本文介绍了在Cesium中加载GeoTIFF数据的两种主要方法。首先解析了核心前置知识,包括坐标系匹配、数据类型区分和性能优化。方法1详细演示了如何将RGB影像GeoTIFF转换为Cesium影像图层,包含从数据解析到Canvas转换的完整代码流程。方法2则针对高程数据(DEM)处理,展示了前端直接解析构建地形网格的方案。文章提供了完整的代码示例和关键实现要点,适用于卫星影像、专题图和数字高程模型等多种GeoTIFF数据类型在三维场景中的可视化需求。

大剑师兰特的GIS世界 395

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

手把手教你完成基于matlab的LSTM时间序列分析

时间序列是按时间顺序组织的数字序列,是数据分析中重要的处理对象之一。时间序列的主要特点是数据获取方式一般具有客观性,能反映某种现象的变化趋势或统计指标,进而预测未来走向,这在本质上也是一个回归预测的问题。长短时记忆网络(Long Short-Term Memory,LSTM)是一种循环神经网络,适合处理有更长时间跨度的内部记忆,被广泛应用于时间序列分析,能够保持数据的内在持续性,反映数据的细粒度走势,具有良好的预测效果。

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

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

温度PID控制PLC程序

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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

余额充值