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)
上一篇 2022年7月23日 下午6:36
下一篇 2022年7月23日 下午6:46


相关推荐

  • md5算法大作战推html5版本,MD5大作战

    md5算法大作战推html5版本,MD5大作战MD5 大作战 是 Cgfan 网站站长 Rinick 基于 MD5 加密技术做出的一种 FLASH 游戏 输入两个字符串 系统就会自动根据这两个字符串的 MD5 码来评定各自的属性 技能 天赋 1 30 版以后废除天赋设定 等数据 并展开这两个 人 之间的对战 中文名 MD5 大作战性质一种 FLASH 游戏作者 Cgfan 网站站长 Rinick 技术基于 MD5 加密技术定义不同的字符串 属性有高低 MD

    2026年3月26日
    3
  • DFS应用——寻找欧拉回路

    DFS应用——寻找欧拉回路0 README0 1 本文总结于数据结构与算法分析 源代码均为原创 旨在理解 DFS 应用 寻找欧拉回路 的 idea 并用源代码加以实现 源代码 我还没有找到一种有效的数据结构和 DFS 进行结合 往后会 po 出 1 欧拉回路 1 1 欧拉回路定义 我们必须在图中找出一条路径 使得该路径对图的每条边恰好访问一次 如果我们要解决 附加的问题 那么我们就必须找到一个圈 该圈恰好

    2026年3月18日
    2
  • c语言的单片机delay延时函数详解

    c语言的单片机delay延时函数详解c语言及单片机delay延时函数延时函数1、是什么2、为什么3、用在哪里?4、怎么做1、循环延时延时函数延时函数,作为一种常用函数,在不同的领域有不同的用处。而在嵌入式以及C语言的编写中,我们常常遇到需要自己来编写延时函数的情况,这种情况之下,了解其原理就显得必要。1、是什么简单来说,延时函数的目的就在于等,实际上就是要等一段时间再来执行接下来的代码。而这种简单的等,又可以采用多种方法来实现。例如:名称描述循环采用for或者while循环,让计算机跑无用的代码,从而达到延时的

    2022年5月5日
    61
  • OpenClaw安装避坑指南

    OpenClaw安装避坑指南

    2026年3月13日
    5
  • 利用PySpark统计相邻字符串对出现的次数

    利用PySpark统计相邻字符串对出现的次数

    2021年11月23日
    67
  • iOS 根据已知NSDictionary的value找key[通俗易懂]

    iOS 根据已知NSDictionary的value找key[通俗易懂]NSString *objectId;   NSDictionary *userDic= @{@”11″:@”aaa”,@”22″:@”fff”,@”33″:@”已知道的value”,@”44″:@”ccc”};  [userDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){ 

    2022年7月23日
    10

发表回复

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

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