kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
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.
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.
OutputFor 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 kmp预处理Next数组。然后遍历每一位。dp[i]=dp[Next[i]]+1 即可
#include<stdio.h>
const int maxn=;
#define mod 10007
int _,n,Next[maxn],d[maxn];
char s[maxn]; void prekmp() {
int i,j;
j=Next[]=-;
i=;
while(i<n) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int main() {
for(scanf("%d",&_);_;_--) {
scanf("%d%s",&n,s);
prekmp();
d[]=;
int sum=;
for(int i=;i<=n;i++) {
d[i]=d[Next[i]]+;
sum+=d[i]%mod;
}
printf("%d\n",sum%mod);
}
}
kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- 开发环境入门 linux基础 (部分)while for 函数 计划任务
while循环 while do 动作 done 需要无限循环时我们会选择while : echo -e 格式处理,将\n的意义不变. exit 指退出执行程序 break 指跳出本层循环 conti ...
- Oracle 10g RAC全库flashback
因业务原因,今天需要做一次全库flashback.以下是操作全过程: 1.确认主库是否能flashback到需要的时间点 在节点1上执行: SQL> alter session set nls_ ...
- Eclipse: “The import java.io cannot be resolved”
检查一下选项: 重点看jdk的绑定 43down voteaccepted Check your Eclipse preferences: Java -> Installed JREs. The ...
- 【Android 多媒体应用】使用 VideoView 播放视频
1.MainActivity.java import android.os.Bundle; import android.support.v7.app.AppCompatActivity; impor ...
- [codevs1159]最大全0子矩阵(悬线法)
解题关键:悬线法模板题.注意此模板用到了滚动数组. #include<cstdio> #include<cstring> #include<algorithm> # ...
- 深入理解asp.net中的 __doPostBack函数
前段时间做一个.net网站的时候,用到了模拟前端按钮刷新updatePanel进行局部刷新的时候,遇见了这个问题,当时没顾上记下来,查看网上资料,记下来留着以后查看. 很早以前,当我刚接触asp.NE ...
- linq 初步认识
linq to sql 类 介绍: linq如果不能用的话 重装一下vs就好了 LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言 ...
- IO流对文件的读取操作
/*1. 在当前项目的根目录下有一个名为“info.txt”的文件,里面存放的内容如下(可手动创建录入,不需要使用IO流): 2. 利用IO流的知识读取info.txt文件的内容, 在控制台上打印大写 ...
- Mind Map-在线软件(转)
From http://blog.sina.com.cn/s/blog_74b687ac0102dtp1.html 第一款:http://tu.mindpin.com/ 用非常简单, 先用Email ...
- linux中的管道命令
很有用的一个命令,用法如下: A | B 是把A命令的输出作为B命令的输入. 比如我想查看一下我在终端输入过的命令,可以这样: history | less