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 ...
随机推荐
- 2015广州强网杯(Misc)
单身狗: 下载图片 被一只狗挡住了的二维码,用图片处理软件把上面两个正方形随便一个覆盖狗的地方 我直接用美图秀秀处理一下,扫一下就得到flag
- 从一个简单的小实例分析JSP+Servelt与JSP+Struts2框架的区别
最近在学struts2,struts2相比以前的JSP+Servlet,在处理流程上的更简单,我们就一个小实例来具体分析一下. 实例内容如下: 实现一个简单的注册页面包括:用户名.密码.重复密码.年龄 ...
- python笔记--4--面向对象
面向对象 Python中对象的概念很广泛,Python中的一切内容都可以称为对象,除了数字.字符串.列表.元组.字典.集合.range对象.zip对象等等,函数也是对象,类也是对象. 在Python中 ...
- ActionBarActivity的使用注意事项
1.调用getActionbar()方法返回为空的解决方法 此activity是设计来支持低版本系统用actionbar的,低版本没有getActionbar() 需要使用 getSupportAct ...
- Eclipse安装Web/JavaEE插件、Eclipse编写HTML代码
1 Eclipse没有Web插件和JavaEE插件咋整 1.1 在Eclipse中菜单help选项中选择install new software选项 1.2 在work with 栏中输入 http: ...
- oracle错误汇总解决
1.ORA-12514 http://blog.sina.com.cn/s/blog_5007d1b10100oqo8.html
- python3-list列表增删改查合并排序
# Auther: Aaron Fan names = ["aaron", "alex", "james", "meihengfa ...
- 导出Excel解决方案之一NOPI
一.概要 导出Excel这个功能相信很多人都做过,但是实现这个功能解决方案有好几种,今天我未大家介绍一种比较新的,其实也不新了- -!它叫NPOI,可以完美操作EXCEl的导入和导出操作,让我们一起看 ...
- Python pika, TypeError: exchange_declare() got an unexpected keyword argument 'type' 问题修复
网上很多写法都是 type='fanout' 这样的.(这里是基于python=3.6版本, pika=0.13.0 版本) credentials = pika.PlainCredentials(' ...
- JavaScript相关知识和经验的碎片化记录
1.JavaScript提示“未结束的字符串常量”错误解决方法 1.1 JavaScript引用时,使用的字符语言不一致. 比如:<script type=”text/javascript ...