HDU 3336 KMP算法中对next数组的理解「建议收藏」

HDU 3336 KMP算法中对next数组的理解「建议收藏」http://acm.hdu.edu.cn/showproblem.php?pid=3336ProblemDescriptionItiswellknownthatAekdyCoinisgoodatstringproblemsaswellasnumbertheoryproblems.Whengivenastrings,wecanw

大家好,又见面了,我是你们的朋友全栈君。

http://acm.hdu.edu.cn/showproblem.php?pid=3336

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

next数组的值就是记录当前位置向前多少个和这个字符串开头多少个相等,有了这样我们只要记录每个next这对应有多少个,然后加上n个自身的一个匹配就行了。

这个题目的关键还是对kmp的理解,本质上kmp的next就是表示串内关系的一个值。

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char str[200001];
int next[200001];
int rec[200001];
int n;
void get_next()
{
    int i=0,j=-1;
    next[0]=-1;
    while(str[i])
    {
        if(j==-1 || str[i]==str[j])
            next[++i]=++j;
        else
            j=next[j];
    }
}
int main()
{
    int t,i,j;
    int ans;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d",&n);
        scanf("%s",str);
        memset(rec,0,sizeof(rec));
        str[n]='a';//next[0]是-1,可看做补0的空缺。
        get_next();
        for(i=1; i<=n; i++)
            rec[next[i]]++;
        for(i=1; i<=n; i++)
            if(rec[i] > 0)
            {
                ans+=rec[i];
                ans%=10007;
            }
        ans+=n;
        printf("%d\n",ans%10007);

    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163392.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 使用Redis实现优先级队列

    使用Redis实现优先级队列优先级队列是一种如先进先出队列和堆栈数据结构的抽象数据类型。所不同的是每一个元素关联一个“优先级”。优先级高的元素比优先级低的元素优先得到处理。本文讲解如何基于Redis的SORTEDSET数据类型实现优先级队列。SORTEDSET中元素关联一个SCORE,可以按SCORE有序查询元素。优先级队列基本操作实现如下:is_empty:查看队列是否为空。使用EXISTS命…

    2022年9月23日
    1
  • CentOS镜像下载&安装配置&Linux常用命令[通俗易懂]

    CentOS镜像下载&安装配置&Linux常用命令[通俗易懂]目录1.linuxcentos7镜像下载2.创建虚拟机3.正式安装CentOS74.远程工具Xshell的使用5.更换国内源6.运行yum命令出现“Existinglock/var/run/yum.pid:anothercopyisrunningaspid…”解决方法​7.Linux常用命令1.linuxcentos7镜像下载下载地址:http://mirrors.aliyun.com/centos/7/isos/x8…

    2022年5月9日
    95
  • 添加网页背景音乐的两种方法是什么_html怎么添加背景音乐

    添加网页背景音乐的两种方法是什么_html怎么添加背景音乐为网页添加背景音乐的方法一般有两种,第一种是通过普通的标签来添加,另一种是通过标签来添加 1.其中,loop=”-1″表示音乐无限循环播放,如果你要设置播放次数,则改为相应的数字即可2.。 第一种方法当页面打开时音乐播放,如果将页面最小化以后播放音乐会自动暂停,第二种方法则不会出现这种情况,只要不将窗口关闭,它会一直播放 ■  :    是用以插入背景音

    2022年9月16日
    0
  • 机器学习中【回归算法】详解

    机器学习中【回归算法】详解关注微信公众号【Microstrong】,我写过四年Android代码,了解前端、熟悉后台,现在研究方向是机器学习、深度学习!一起来学习,一起来进步,一起来交流吧!本文同步更新在我的微信公众号里,地址:https://mp.weixin.qq.com/s?__biz=MzI5NDMzMjY1MA==&amp;mid=2247483935&amp;idx=1&amp;sn=5e1c55c76…

    2022年8月21日
    3
  • Mac datagrip 2022激活码【2022最新】

    (Mac datagrip 2022激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html2KLKA7BQFO-eyJsaWN…

    2022年4月1日
    545
  • 一个依赖搞定 Spring Boot 反爬虫,防止接口盗刷!

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 责编:乐乐 链接:oschina.net/news/112586/kk-anti-reptile-released …

    2021年6月24日
    100

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号