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


相关推荐

  • mysql 10051_Zabix的10051端口无法启动如何解决?

    mysql 10051_Zabix的10051端口无法启动如何解决?启动zabbix是显示启动成功,查看端口,却发现没有10051端口1、查看zabbix的日志[root@bogonldap]#cat/tmp/zabbix_server.log’/var/lib/mysql/mysql.sock'(2)2848:20181204:084007.165databaseisdown:reconnectingin10seconds2848:2018…

    2025年8月4日
    3
  • PNP和NPN的区别_pnp和npn的二极管图

    PNP和NPN的区别_pnp和npn的二极管图为什么PNP三极管集电极(C)和发射极(E)反着接,却可以当开关使用?理解NPN和PNP两种类型的三极管原理及电流方向就会明白为什么PNP三极管的集电极和发射极反着接当开关使用。NPN和PNP三极

    2022年8月5日
    9
  • CDO盛行,CIO作何应对?

    CDO盛行,CIO作何应对?

    2022年3月4日
    41
  • UART 接口测试「建议收藏」

    UART 接口测试「建议收藏」串口UART测试程序带传参波特率、奇偶校验、停止位、数据位#include<stdio.h>#include<stdlib.h>#include<string.h>#include<pthread.h>#include<fcntl.h>#include<errno.h>#include<std…

    2022年9月14日
    2
  • 开源跨境电商erp源码_商城java源码

    开源跨境电商erp源码_商城java源码1订单管理本模块支持多平台订单自动下载同步以及多帐号多店铺订单管理,方便用户对销售进行科学、直观的分类管理。包括订单处理,包装验货,称重出库,智能交运,交运日志,快速拣货,快速发货等子模块。2商品管理(SKU)商品管理模块,提供对亚马逊店逊商品进行线下管理的功能,包括但不限于中文名称、英文名称,售价等相应管理3.采购管理采购管理主要对于商品采购、入库、及供应商的设置,并于商品细分,包括采购管理、入库管理和供应商管理模块。4.物流管理此模块主要提供…

    2022年9月20日
    4
  • 数学图形(1.5)克莱线

    数学图形(1.5)克莱线克莱线(Cayley’sSextic)是极坐标方程为:y=4a(cosΘ/3)^3的六次曲线,其中a是一个实数。相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群:367752815克莱线看上去与心形线类似.呵呵,我想说的是,它更像个多了屁眼的屁股。vertices=1000r=10.0t=from(…

    2022年9月13日
    3

发表回复

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

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