KMP算法的综合练习

DP很久没写搞了半天才明白。本题结合Next[]的意义以及动态规划考察对KMP算法的掌握。

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
 
 #include<iostream>  //KMP+DP
#include<memory.h>
using namespace std;
char s[];
int Next[],DP[]; //DP[i]表示子串s[0~i]共含有以s[i]为结尾的前缀的数目
int l; void GetNext(){
int i=,j=-;
Next[]=-;
while(i<l){
if(j==-||s[i]==s[j]){
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
} int main()
{
int n,k,num;
cin>>n;
while(n--){
cin>>l>>s;
GetNext();
num=;
memset(DP,,sizeof(DP));
for(k=;k<=l;k++){
DP[k]=DP[Next[k]]+; //s[i]结尾的前缀数就是自己本身加上以s[Next[i]]结尾的前缀数
num=(num+DP[k])%;
}
cout<<num<<endl;
}
return ;
}

【KMP+DP】Count the string的更多相关文章

  1. 【HDU 3336】Count the string(KMP+DP)

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

  2. Codeforces Beta Round #71 C【KMP+DP】

    Codeforces79C 题意: 求s串的最大子串不包含任意b串: 思路: dp[i]为以i为起点的子串的最长延长距离. 我们可以想到一种情况就是这个 i 是某个子串的起点,子串的长度-1就是最短, ...

  3. P5404-[CTS2019]重复【KMP,dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P5404 题目大意 给出一个字符串\(S\),然后求有多少个长度为\(m\)的串\(T\)满足.无限多个串\(T\) ...

  4. 洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】

    题目链接 洛谷P4591 题解 设\(f[i][j]\)表示前\(i\)个串匹配到位置\(j\)的方案数,匹配一下第\(i\)个串进行转移即可 本来写了\(hash\),发现没过,又写了一个\(KMP ...

  5. 【期望DP】

    [总览] [期望dp] 求解达到某一目标的期望花费:因为最终的花费无从知晓(不可能从$\infty$推起),所以期望dp需要倒序求解. 设$f[i][j]$表示在$(i, j)$这个状态实现目标的期望 ...

  6. Kattis - bank 【简单DP】

    Kattis - bank [简单DP] Description Oliver is a manager of a bank near KTH and wants to close soon. The ...

  7. HDOJ 1501 Zipper 【简单DP】

    HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...

  8. Vijos 1565 多边形 【区间DP】

    描述 zgx给了你一个n边的多边形,这个多边形每个顶点赋予一个值,每条边都被标上运算符号+或*,对于这个多边形有一个游戏,游戏的步骤如下:(1)第一步,删掉一条边:(2)接下来n-1步,每步对剩下的边 ...

  9. Vijos 1451 圆环取数 【区间DP】

    背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...

随机推荐

  1. Unity3D Asset stored 已下载的位置

    Unity3D Asset stored下载资源在本地的什么目录里呢?C:\Users\accountName\AppData\Roaming\Unity\Asset Store

  2. 使用rpm安装mysql的默认目录

    --使用rpm安装mysql的默认目录:1.数据库目录/var/lib/mysql/2.配置文件/usr/share/mysql(mysql.server命令及配置文件)3.相关命令/usr/bin( ...

  3. PHP编写的SVN类

    <?php /** * SVN 外部命令 类 * * @author rubekid * * @todo comment need addslashes for svn commit * */ ...

  4. 使用JQuery获取对象的几种方式

    1.先讲讲JQuery的概念 JQuery首先是由一个 America 的叫什么 John Resig的人创建的,后来又很多的JS高手也加入了这个团队.其实 JQuery是一个JavaScript的类 ...

  5. 富文本文件CKEDITOR增加上传图片功能(.net)

    如题,本身的CKEDITOR控件并没有开启上传图片的功能, 打开图像按钮,只有图像信息和高级两个table选项卡,版本不同,显示略有差异,我的实现是有两种方法都可以添加上传功能, 第一种方法使用CKE ...

  6. Android开发手记(10) 下拉菜单Spinner

    1.自定义Spinner 首先,定义Spinner要显示的项目列表/res/values/arrays.xml <?xml version="1.0" encoding=&q ...

  7. 一个控制台贪吃蛇小游戏(wsad控制移动)

    /******************************************** * 程序名称:MR.DUAN 的贪吃蛇游戏(链表法) * 作 者:WindAutumn <flutti ...

  8. Linux && vim 批量替换

    Linux批量文件的字符串替换 sed -i "s/oldstring/newstring/g" `grep oldstring -rl path` vim多行替换::1,2s/s ...

  9. Date与Calendar

    Date date=new Date();//现在时间 Date date1=new Date(1000);//格林威治时间1997/01/01开始算,后面的是毫秒 Calendar calendar ...

  10. C++ Primer 5th 第7章 类

    类的基本思想是数据抽象和封装,定义类就是定义一个抽象数据类型. 类中的所有成员必须在类中声明,也即默认定义在类中的成员全部为声明,除非显式的定义成员函数的函数体.成员函数是在类中声明的,定义可以在类内 ...