hdu3336 Counting the string kmp的next数组的应用
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3336/
题意就是要求一个字符串的所有前缀在字符串中出现的次数之和,我们容易想到kmp中的next数组,next[i]=j,表示存在一个长度为j的前缀与长度为j的后缀相同,本题的结果就是要对每一位的next数组都向前回退,回退的次数就是长度不同的前缀出现的次数。还可以采用dp的策略。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x)
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x3f3f3f3f
#define maxn 1000010
int n,m,t;
int nxt[maxn];
char s[maxn];
void getnxt()
{
int i=-,j=;
nxt[]=-;
while(j<n)
{
if(i==-||s[i]==s[j])
{
i++,j++;
nxt[j]=i;//不能用kmp优化的nxt,那样会损失数量
}
else i=nxt[i];
}
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
std::ios::sync_with_stdio(false);
scan(t);
while(t--)
{
scan(n);
scanf("%s",s);
getnxt();
int ans=;
f(i,,n)//查看以i-1为截至位置的公共前缀后缀
{
int j=i;
while(j>)
{
ans++;//初始时本身还要算一次
if(ans>)ans-=;
j=nxt[j];
}
}
pf("%d\n",ans);
}
}
dp:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int const MOD = ;
int const MAXN = ;
char s[MAXN];
int next[MAXN],dp[MAXN];
inline void Get_Next(int n){
memset(next,,sizeof(next));
for(int i = ;i < n;i++){
int j = next[i];
while(j && s[i] != s[j]) j = next[j];
if(s[i] == s[j]) next[i + ] = j + ;
else next[i + ] = ;
}
}
int main(){
int T;
while(~scanf("%d",&T)){
while(T--){
int n;
scanf("%d",&n);
scanf("%s",s);
Get_Next(n);
for(int i = ;i < n;i++){
dp[i] = ;
}
dp[] = ;
int sum = ;
for(int i = ;i <= n;i++){
dp[i] = dp[next[i]] + ;
sum = (sum + dp[i]) % MOD;
}
printf("%d\n",sum);
}
}
return ;
}
hdu3336 Counting the string kmp的next数组的应用的更多相关文章
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- HDU3336 Count the string(kmp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
- Count the string[KMP]HDU3336
题库链接http://acm.hdu.edu.cn/showproblem.php?pid=3336 这道题是KMP的next数组的一个简单使用,首先要理解next数组的现实意义:next[i]表示模 ...
- 求最长公共前缀和后缀—基于KMP的next数组
KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...
- KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words
题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out ---> ...
随机推荐
- ERROR 1176 (42000): Key 'XXX' doesn't exist in table 'XXX'报错处理
MySQL5.7对sql语句强制使用索引查询时报错如下: 解决:这里的id字段是表的主键,查看别人的经验贴得知是语法错误,参考链接https://stackoverflow.com/questions ...
- Autotestplat体验中心
web端 移动端 可戳[阅读原文]进行体验
- doctrine queryBuilder
为了能够方便的切换数据库,我们有必要使用doctrine的queryBuilder, 但是估计很多人都是不喜欢的(我也是),之前尝试用的时候,发现在doctrine定义的SELECT语法中并没有CON ...
- 混乱中的ICO平台,会不会是下一个P2P的重灾区?
当众多巨头和创业者还在为共享打车.共享单车.VR.IP化.互联网金融沉迷时,一种全新的"众筹"正在造就一个又一个暴富神话.其名为ICO,即首次代币众筹,一般指区块链初创项目在众筹平 ...
- python爬虫之数据加密解密
一.什么叫数据加密 数据加密是指利用加密算法和秘钥将明文转变为密文的过程. 二.数据加密的方式 1.单向加密 指只能加密数据而不能解密数据,这种加密方式主要是为了保证数据的完整性,常见的加密算法有MD ...
- 使用Navicat或者其他数据库工具连接阿里云EDS(数据库服务器)实例过程详解
使用Navicat或者其他数据库工具连接阿里云EDS(数据库服务器)实例过程详解 背景:这几天从阿里云上面购买了云服务器,最垃圾的那种,还送oss和EDS数据库服务器,只不过EDS数据库服务器只有一个 ...
- py装饰器,生成器,迭代器
emmmmm....看了好久才能大概的看懂一点关于装饰器的内容...import sys # 引入sys模块import timeimport functoolsfrom functools impo ...
- Python 存储数据到json文件
1 前言 很多程序都要求用户输入某种信息,程序一般将信息存储在列表和字典等数据结构中. 用户关闭程序时,就需要将信息进行保存,一种简单的方式是使用模块json来存储数据. 模块json让你能够将简单的 ...
- python3 flask shell
python shell来操作flask flask shell 报错: from flask_bootstrap import BootstrapImportError: No module nam ...
- 关于javascript 的reduce方法
作为一个前端菜鸟,觉得资料比较好,特地分享一下~~ reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. 你一定也和我一样看的有点 ...