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 ---> ...
随机推荐
- Python字符串编码——Unicode
ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte).也就是 ...
- GoLand2019.03破解与汉化
1.准备工作 (请认真的做好准备工作,否则中途可能会操作失败.) GoLand是JetBrains公司发布的商业版的GO语言编辑器(收费的),本屌目前还没钱购买正版,所以本次教程是以Windows平台 ...
- java ThreadPoolExecutor初探
导读:线程池是开发中使用频率比较高的组件之一,但是又有多少人真正了解其内部机制呢. 关键词:线程池 前言 线程池是大家开发过程中使用频率比较高的组件之一,但是其内部原理又有多少人真正清楚呢.最近抽时间 ...
- java集合-set
#java集合-set Map用于存储key-value的映射,其中key的值是不能重复的.并且还需要正确的覆写equals方法和hashCode方法 如果我们只需要存储不重复的key,并不需要存储对 ...
- python django 之 django自带的分页
1. 例1: 基础的分页 1). vim app01/views.py def users(request): from django.core.pagina ...
- 利用matplotlib进行数据可视化
matplotlib是python中的一个画图库,继承了matlib(从名字上也看得出来)的优点和语法,所以对于熟悉matlib的用户来说是十分友好的. pylab和pyplot 关于pylab和py ...
- sql -- update表子查询、多条件判断case when
表结构: 需求 思路: 求出平均数 select avg(user_total) as avg from user_level 更新他的等级 update user_level set user_ra ...
- 7-42 jmu-python-找字符 (15 分)
输入一个字符串及字符,输出第一次出现该字符的位置. 输入格式: 行1:输入字符串 行2:输入一个字符 输出格式: 找到,输出对应位置,格式index=X的, X表示查找到位置 找不到,输出can't ...
- CF 1305E. Kuroni and the Score Distribution
题目大意:题目给定两个数n和m(1<=n<=5000,0<=m<=1e9)要求构造一个数列A,A中元素 大于等于1,小于等于1e9且满足严格递增 满足ai+aj=ak的(i,j ...
- czC#02
1.out参数 out参数要求在方法的内部必须为其赋值 using System; using System.Text; namespace Demo { class Program { //返回一个 ...