Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 14096    Accepted Submission(s): 6462

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数组可以表示以当前坐标的前一个字符结尾的字符串的前缀和后缀有多少个字符相等

举个例子,有一个字符串

abcdabdabcdab

i            0    1    2    3    4    5    6    7    8    9    10   11   12   13

a    b    c    d    a    b   d    a    b    c    d     a     b

nex[i]  -1    0    0    0    0    1    2    0    1    2    3     4     5     6

dp[i]     0    1    1    1    1    2    2    1    2    2    2     2     3     3

dp[i]=dp[nex[i]]+1

dp[i]表示第i个字母出现次数(与前缀匹配情况下),+1代表的是前缀出现次数加1,dp总和即为前缀出现次数和

#include<iostream>
#include<string.h>
#define inf 10007
using namespace std;
int nex[200001], dp[200001];
void getnext(char s[],int len)
{
int i=0,j=-1;
nex[0]=-1;
while(i<len)
{
if(j==-1||s[i]==s[j])
nex[++i]=++j;
else j=nex[j];
}
}
int main()
{
int t,n;
scanf("%d",&t);
char s[200001];
while(t--)
{
scanf("%d%s",&n,s);
getnext(s,n);
int cnt=0;
dp[0]=0;
for(int i=1;i<=n;i++)
{
dp[i]=(dp[nex[i]]+1)%inf;
cnt+=dp[i]%inf;
}
printf("%d\n",cnt%inf);
}
return 0;
}

HDU 3336 Count the string(next数组运用)的更多相关文章

  1. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  3. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 3336 Count the string(next数组)

    题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...

  6. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  8. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. HDU 3336——Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. 使用Swagger自动生成API文档

    ⒈添加pom依赖 <!-- Swagger核心包,用于扫描程序生成文档数据 --> <dependency> <groupId>io.springfox</g ...

  2. canves绘制北京地铁线路图,包括线路绘制,优先路线,单路径选择。

    canves绘制北京地铁线路图,包括线路绘制,优先路线,单路径选择. 即将推出,后台涵盖各种语言,php,C#,java,nodejs等.

  3. 如何能让MAC和PC都能读写移动硬盘

    Macbook Pro 移动硬盘 希捷硬盘就不用,有专业适用于苹果的软件. 1.在“LaunchPad”中找到并打开“磁盘工具”,在“磁盘工具”中可以看到移动硬盘的几个分区 2.选择一个分区后,选择“ ...

  4. Linker Scripts3--MEMORY Command

    1.前言 链接器的默认配置允许所有有效内存的分配,你可以使用MEMORY命令来重新定义它 2.MEMORY命令 MEMORY命令描述了一个内存块的位置和大小.你可以用它来描述哪块内存区域可以被链接器使 ...

  5. HAProxy详解(一):HAProxy介绍【转】

    一.高性能负载均衡软件HAProxy介绍: 随着互联网业务的迅猛发展,大型电商平台和门户网站对系统的可用性和可靠性要求越来越高,高可用集群.负载均衡集群成为一种热门的系统架构解决方案.在众多的负载均衡 ...

  6. BCG界面库

    之前用过BCG界面库中的表格控件,深感其强大,现在再来用一下其它的. 一.   关于BCGControlBar. BCGControlBar是一个基于MFC的扩展库,您可以通过完全的用户化操作构成一些 ...

  7. JavaScript中的this -- 好像很有道理版

    函数调用 首先需要从函数的调用开始讲起. JS(ES5)里面有三种函数调用形式: func(p1, p2) obj.child.method(p1, p2) func.call(context, p1 ...

  8. java的小数比较反例

    double num = 0.3; System.out.println(num); System.out.println(num - 0.2); System.out.println(num - 0 ...

  9. CentOS7.5从零安装Python3.6.6

    ps:环境如标题 安装可能需要的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlit ...

  10. ajax控件无法使用 iis配置及web修改(转载)

    1.Web.config配置问题:将Web.config中的相关节配置成如下,然后重新编译你的程序:<httpHandlers><remove verb="*" ...