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. linux开机自动启动脚本

      前言linux有自己一套完整的启动 体系,抓住了linux启动 的脉络,linux的启动 过程将不再神秘.阅读之前建议先看一下附图.本文中假设inittab中设置的init tree为:/etc/ ...

  2. CentOS6.3 Firefox安装FlashPlayer

    这段时间搞搞CentOS,我自己用的版本是CentOS6.3,基本上都差不多,过程都一样,主要说一下步骤 1.从Adoble官网下载FlashPlayer插件,下载地址:http://get.adob ...

  3. ios10下,手机连接xcode控制台不显示日志,解决办法

    按照下面设置: run- > environment variables->添加 OS_ACTIVITY_MODE ,value内容为空

  4. Config配置文件读写

    config文件读写操作(文字说明附加在程序中) App.config文件 <?xml version="1.0" encoding="utf-8" ?& ...

  5. spring源码分析

    编译问题 spring-4.0.5.release编译是用jdk8编译的,为啥可以运行在jdk7的环境? 源码分析 spring源码分析,由一个点各个击破,比如依赖注入,autowired. spri ...

  6. 仿写Windows7桌面和任务栏 HTML5+CSS3+Jquery实现

    过去一段时间零零散散的自学了一点点jquery的相关用法,基本上属于用到哪个了,就去查然后就学一点,没有系统的学过,深入的用法也不是特别了解,毕竟javascript基础就比较薄弱.经过一段时间的零敲 ...

  7. 基于脚本的动画的计时控制(“requestAnimationFrame”)(转)

    requestAnimationFrame 方法的支持,该方法通过在系统准备好绘制动画帧时调用该帧,从而为创建动画网页提供了一种更平滑更高效的方法.在此 API 之前,使用 setTimeout 和  ...

  8. BOM和DOM的联系和区别

    BOM中的对象 Window对象: 是整个BOM的核心,所有对象和集合都以某种方式回接到window对象.Window对象表示整个浏览器窗口,但不必表示其中包含的内容. Document对象: 实际上 ...

  9. java加载配置文件

    有时候一些变量可能会变,但直接写在程序代码中不好,因为有时候需要改的时候要改动源代码,这时候可以使用配置文件来管理.比如数据库中的端口和密码. 1. 把.properties配置文件放在src目录下. ...

  10. [转]简述volatile

    volatile int i=10; int j = i; ... int k = i; volatile 告诉编译器i是随时可能发生变化的,每次使用它的时候必须从i的地址中读取,因而编译器生成的可执 ...