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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2021韩顺平图解linux_狗剩学习笔记

    2021韩顺平图解linux_狗剩学习笔记韩顺平图解Linux全面升级https://www.bilibili.com/video/BV1Sv411r7vdP001_韩顺平图解Linux全面升级_课程内容20:37P002_韩顺平图解Linux全面升级_应用领域05:05P003_韩顺平图解Linux全面升级_概述16:37P004_韩顺平图解Linux全面升级_Linux与Unix18:09P005_韩顺平图解Linux全面升级_vmware15.5安装17:36P006_韩顺平图解

    2022年5月18日
    38
  • 详解Java中的Spring框架

    详解Java中的Spring框架详解Spring什么是SpringSpring的优点Bean容器Bean的注解Bean属性Bean作用域Bean的生命周期Bean的实例化IoC(InversionofControl)和DI(DedendencyInjection)IoC(控制反转)DI(依赖注入)AOP什么是SpringSpring是分层的JavaSE/EEfull-stack轻量级开源框架,以IoC(InverseofControl,控制反转)和AOP(AspectOrientedProgramming

    2022年7月7日
    15
  • 万能密码大全[通俗易懂]

    万能密码大全[通俗易懂]aspaspx万能密码1:”or”a”=”a2: ‘)or(‘a’=’a3:or1=1–4:’or1=1–5:a’or’1=1–6:”or1=1–7:’or’a’=’a8:”or”=”a’=’a9:’or”=’10:’or’=’or’11:1

    2022年6月15日
    157
  • session.setAttribute报错_java string contains方法

    session.setAttribute报错_java string contains方法HTTPSession在setAttribute时,保存的对象是否需要序列化?查看StandardSession源码中,在setAttribute()中有如下代码if((manager!=null)&amp;&amp;manager.getDistributable()&amp;&amp;!isAttributeDistributable(name…

    2022年10月9日
    0
  • 黑苹果安装教程OC引导「建议收藏」

    黑苹果安装教程OC引导「建议收藏」小白安装指南1.从零开始,自制EFI,安装黑苹果2.网络下载EFI(80%可能你根本找不到能用的)3.黑苹果安装过程卡代码4.安装好黑苹果后引导修复5.修改BIOS5.其他可能有帮助的链接首先声明,我也是小白,只是总结一下我安装黑苹果过程中参考过的教程。以下内容如有帮助本人深感欣慰。最开始装黑苹果心里特别没底,不知从何下手,那先看1.从零开始,自制EFI,安装黑苹果推荐从零学习,大约需要5个小时自己就能安装黑苹果了。1.视频中使用的是OC0.6.5版本,现在已经升级到0.6.6但是并没有什么区

    2022年6月22日
    443
  • 淘宝开源工具:Orztop

    淘宝开源工具:Orztop

    2022年3月11日
    37

发表回复

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

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