题目链接: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数组的应用的更多相关文章

  1. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  2. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  3. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  4. HDU3336 Count the string(kmp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  5. 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 ...

  6. [KMP][HDU3336][Count the string]

    题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...

  7. Count the string[KMP]HDU3336

    题库链接http://acm.hdu.edu.cn/showproblem.php?pid=3336 这道题是KMP的next数组的一个简单使用,首先要理解next数组的现实意义:next[i]表示模 ...

  8. 求最长公共前缀和后缀—基于KMP的next数组

    KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...

  9. KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words

    题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out   ---> ...

随机推荐

  1. Flask向模板中JS传值简便方式

    后台传值: return render_template('statistics/numberofuserlogin/login_number.html', result_json = json.du ...

  2. JAVA SE Lesson 1

    1.  类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成对象(Object).对象又叫做实例(Instance).2.  类由两大部分构成:属性以及方法.属性一般用 ...

  3. mysqldump免密备份方法

    注意:1.暂时只试验了root用户     2.暂时只试验了5.6和5.7两个版本 1.我用的root用户,先进入家目录 cd ~ 2.vim .my.cnf #在家目录添加该文件 [mysqldum ...

  4. vmware增加新硬盘无需重启生效

    echo "scsi add-single-device 2 0 2 0" > /proc/scsi/scsi # echo "scsi add-single-de ...

  5. nginx图片过滤处理模块http_image_filter_module安装配置

    http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...

  6. grpc调试工具

    grpcurl 和 grpcui 都是调试grpc的利器,前者用于命令行,类似curl工具:后者是以web的形式进行调试的,类似postman工具. 有了这两款工具,我们不用写任何客户端代码,也能方便 ...

  7. 《HelloGitHub》第 47 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  8. cssy元素居中的方法有哪些?

    css的元素居中 各位小伙伴们在努力写网页的时候有没有遇到过这样的一个问题呢? 在写的时候发现他不居中,可是要分分钟逼死强迫症的啊! 别急,我来啦 哈哈哈 今天就带来三种css的元素居中的方法 第一种 ...

  9. swoole(1)使用docker安装swoole环境

    1.下载镜像 pull php 镜像 docker pull php:7.3-alpine3.8 创建容器 docker run -it --name test php:7.3-alpine3.8 s ...

  10. Yuchuan_linux_C 编程之八 文件操作相关函数

    一.整体大纲 st_mode整体介绍: st_mode详细介绍: 二. Linux文件操作相关函数 1. stat 作用:获得文件信息,也可以获取文件大小. 头文件 #include <sys/ ...