hdu_3336: Count the string(KMP dp)
题意:求给定字符串中,可以与某一前缀相同的所有子串的数量
做这道题需要明白KMP算法里next[]数组的意义
首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长公共元素长度。(nex[i]表示,在S[1~i](在标号从1开始的情况下)这个字串中,前缀后缀最长公共元素长度)
然后使用一数组dp[],dp[x]中存放 S[1~x]中共含有 以S[x]结尾的&&与某一字串相同 字串的个数
这样递推一下,然后把所有dp[i]求个和就是ans了
#include<bits/stdc++.h>
using namespace std; const int N=2e5+;
const int mod=;
char T[N],S[N];
int nex[N];
int dp[N];
int slen; void getNext()
{
int j,k;
j=;k=-;nex[]=-;
while(j<slen)
if(k==-||S[j]==S[k])
nex[++j]=++k;
else
k=nex[k];
}
void printNext()
{
for(int i=;i<slen;i++)
printf("%3c",S[i]);
puts("");
for(int i=;i<=slen;i++)
printf("%3d",nex[i]);
puts("");
for(int i=;i<=slen;i++)
printf("%3d",dp[i]);
puts("");
} int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d",&slen);
scanf("%s",S);
getNext();
int sum=;
for(int i=;i<=slen;i++)
{
dp[i]=dp[nex[i]]+;
sum=(sum+dp[i])%mod;
}
// printNext();
printf("%d\n",sum);
}
}
/*
55
6
111111
6
121212
6
123123
*/
hdu_3336: Count the string(KMP dp)的更多相关文章
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
- 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)
2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- HDU3336(KMP + dp)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 - Count the string(KMP+递推)
题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...
- hdu 3336 Count the string(next数组)
题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...
随机推荐
- python List和String 转换注意
不能用str(list),t=['\x87\xe9\xa5\xb0\xef\xbc']In [28]: str(t)Out[28]: "['\\x87\\xe9\\xa5\\xb0\\xef ...
- iOS项目评估报告
1.整体项目无分层概念,结构混乱,代码耦合严重. 影响:后期扩展困难,维护困难. 解决方案:1.整体采用mvc模式. 2.在原来的基础再抽离出业务层 3.业务层按模块管理,合理分层分包. 4.做好共用 ...
- File字节流
1. File f = new File("文件路径") 注意:相对路径:非web项目的相对都是以项目为起点.(src/a/txt(建议) 绝对路径:f: ...
- Java下一个简单的数据库分库帮助类
简介 前面两篇文章主要讲了数据库读写分离和分表分库的一些问题,这篇文章主要讲一下我个人实现的一个分表分库项目. 在此之前,我有写过一个.Net的分库,最近在做Java的项目,就顺便做出一 ...
- string services
string通用字符串操作: re,正则表达式 difflib,比较序列 stringIO:以文件的方式来读和写字符串 CstringIO:更快捷的stringIO版本 textwrap:文本包装和填 ...
- JavaScript中的string对象及方法
string对象 string对象的两种创建 var a="hello"; var b=new String("hello"); //下面是方法 //charA ...
- springmvc 之 DispatcherServlet
DispatcherServlet说明 使用Spring MVC,配置DispatcherServlet是第一步. DispatcherServlet是一个Servlet,所以可以配置多个Dispat ...
- FreeMaker开发教程
FreeMaker简介 FreeMaker其实是一种比较简单的网页展示技术,说白了就是网页模板和数据模型的结合体.这种结合模式的好处就是,分离了网页界面设计人员和编程人员的工作,让他们各司其职. 据个 ...
- JS监听div的resize事件
原文地址:http://zhangyiheng.com/blog/articles/div_resize.html 需求 开发过程中经常遇到的一个问题就是如何监听一个div的size变化. 比如我用c ...
- Vulkan Tutorial 21 Staging buffer
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 顶点缓冲区现在已经可以正常工作,但相比于显卡内部读取数据, ...