Count the string

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

Total Submission(s): 3351    Accepted Submission(s): 1564

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

Author foreverlin@HNU

Source HDOJ Monthly Contest – 2010.03.06

Recommend lcy

#include<stdio.h>
#include<string.h>
#define gs 200010
int N;
char str[gs];
int dp[gs],next[gs];
void getnext()
{
next[]=;
int k=;
for (int i=;i<N;i++)
{
while (k> && str[k]!=str[i]) k=next[k];
if (str[k]==str[i]) k++;
next[i]=k;
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&N);
scanf("%s",str);
getnext();
dp[]=;
int ans=;
for (int i=;i<N;i++)
{
if (next[i]==) dp[i]=;
else dp[i]=dp[next[i]-]+;
(ans+=dp[i])%=;
}
printf("%d\n",ans);
}
return ;
}

Count the string[HDU3336]的更多相关文章

  1. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  2. 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) ...

  3. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  4. 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 ...

  5. Count the string -- HDOJ 3336

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

  6. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

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

  7. HDU 3336 Count the string(next数组运用)

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

  8. (KMP)Count the string -- hdu -- 3336

    http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...

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

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

随机推荐

  1. Java 23种设计模式

    转自: http://zz563143188.iteye.com/blog/1847029 ; i<count; i++){ list.add(new MailSender()); } } pu ...

  2. PHPStorm+Wamp+Xdebug+Windows7调试代码

    Wamp 集成环境 PHPStorm+Xdebug 调试代码 2013.04.16 花了两个小时时间终于 , 配置成功了 ! 我的开发环境如下 , 其它环境也可以参考我的配置 开发环境 : Windo ...

  3. DCMTK3.6.0(MD支持库)安装说明

    一.运行环境:WIN7 32bit + VisualStudio2008 + dcmtk3.6.0 + Cmake2.8.8 或者 WIN7 64bit 二.准备工作: 1)MD/MT的知识储备: / ...

  4. android设置图片自适应控件大小

    在XML文件的ImageView属性中加上:android:scaleType="fitXY"

  5. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  6. Bellman-Ford算法

    #include<stdio.h> #define max 0xffffff ][]; //图的邻接矩阵 ]; int n;//顶点个数 int m;//边个数 struct Edge { ...

  7. Android 中的AsyncTask

    在后台下载图片,下载完成后更新UI是一个很常见的需求.在没有AsyncTask类之前,我们需要写许多thread和Handler的代码去实现这个功能,有了AsyncTask,一切变得简单了.下面摘抄谷 ...

  8. iOS CoreData 的级联删除等操作

    关于CoreData 的基本操作在网上有一些中文资料,但是这些资料大多没有涉及CoreData的详细操作,只是简单的演示了最基本用法.像级联删除这种最基本的数据库操作都没有提到.今天在网上看到了一些英 ...

  9. HDU 5512 Pagodas (gcd)

    题目:传送门. 题意:t组数据,每组数据给定n,a,b,a!=b,在[1,n]的这些点中,每次选取a+b或a-b或b-a点,选取过的点在下次选取的时候可以当做ab来用继续选取,谁不能继续选取谁就输,问 ...

  10. UVA11806-Cheerleaders(容斥原理+二进制)

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...