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


相关推荐

  • 简易SDRAM控制器的verilog代码实现

    简易SDRAM控制器的verilog代码实现SDRAM是每隔15us进行刷新一次,但是如果当SDRAM需要进行刷新时,而SDRAM正在写数据,这两个操作之间怎么进行协调呢?需要保证写的数据不能丢失,所以,如果刷新的时间到了,先让写操作把正在写的4个数据(突发长度为4)写完,然后再去进行刷新操作;而如果在执行读操作也遇到需要刷新的情况,也可以先让数据读完,再去执行刷新操作。思路:SDRAM控制器包括初始化、读操作、写操作…

    2022年7月25日
    9
  • GridView DataFormatString 的用法总结

    VS2005下BoundField列如何使用DataFormatString属性  HtmlEncode=”False” 完整日期时间格式(longdate+longtime)dddd,MMMMdd,yyyyHH:mm:ssg一般格式(shortdate+shorttime)MM/dd/yyyyHH:mmG一般格式(shortdat

    2022年4月7日
    36
  • vuex的使用之mapGetters[通俗易懂]

    vuex的使用之mapGetters[通俗易懂]vue项目中,经常会使用到vuex,vuex是vue的一个状态管理。本文简单总结一下:vuex中mapGetters的使用。如果一个变量或对象需要在多个页面和组件中使用,那么,可以使用mapGetters。一.vuex中声明变量个方法1.在state中声明:state:{freeShipping:cookie.get(‘freeShipping’),}2.在mutations中书写方法:mutations:{updatefreeShipping(state,fre

    2022年5月20日
    117
  • PyCharm点击设置没反应,无法进行设置「建议收藏」

    PyCharm点击设置没反应,无法进行设置「建议收藏」首先检查下是不是装了中文汉化包resources_cn.jar如果有的话,解决办法:1.更换一个汉化包或者将原来的resources_en.jar也放进lib目录下                                    2.将汉化包都删除,只留下原版的resources_en.jar   …

    2022年8月29日
    1
  • 一文搞懂双亲委派模型「建议收藏」

    一文搞懂双亲委派模型「建议收藏」类加载器虚拟机设计团队把类加载阶段中的“通过一个类的全限定名来获取此类的二进制字节流”这个动作放到Java虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类。实现这个动作的代码模块称为“类加载器”。从Java虚拟机的角度来讲,只存在以下两种不同的类加载器:启动类加载器(BootstrapClassLoader),使用C++实现,是虚拟机自身的一部分所有其它类的加载…

    2022年4月19日
    49
  • cardboard应用_cardboard怎么用

    cardboard应用_cardboard怎么用GoogleCardboard虚拟现实眼镜开发初步(一)虚拟现实技术简介不得不说这几年虚拟现实技术逐渐火热,伴随着虚拟现实设备的价格迅速平民化,越来越多的虚拟现实设备来到了我们眼前,也因此虚拟现实方面的开发离我们也越来越近。这几年迅速崛起的Oculus,其成功就在于拉近了虚拟现实与群众的距离,把原本价格高不可攀的虚拟现实设备放到了我们可以触手可及的位置,Oculus的技术开辟了

    2025年11月2日
    5

发表回复

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

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