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.

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

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的更多相关文章

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

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  5. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

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

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

随机推荐

  1. Python打造一个目录扫描工具

    目标:用Python3写一款小型的web目录扫描工具 功能:1.扫描指定站点 2.指定网站脚本类型来扫描 3.可控线程 4.可保存扫描结果 首先定义一个命令参数的函数 def parse_option ...

  2. navicat for mysql ,mysql版本是8.0的版本,连接数据库报错1251,解决办法。

    我的mysql版本是8.0的版本,因为毕竟新的mysql采用新的保密方式,所以就的似乎不能用,改密码方式: 用管理员身份打开cmd mysql -uroot -p(输入密码)            进 ...

  3. Python之函数目录(自定义函数,内置函数,装饰器,迭代器,生成器)

    1.初始函数 2.函数嵌套及作用域 3.装饰器 4.迭代器和生成器 6.内置函数 7.递归函数 8.匿名函数 9.函数相关定义 10.三元表达式.列表推导式.生成器表达式 11.函数与方法的区别  

  4. ListView显示Sqlite的数据美化版与性能优化

    在上一篇文章中,我们已经实现在listview显示数据库内容的.但是我们listview中,排版不是很好看,所以这篇文章呢,我们来对listveiw进行美化.哈哈,说白了,就是对listview添加一 ...

  5. 获取字符串长度函数length()和hengthb()

    oracle获取字符串长度函数length()和hengthb() lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节 length(string)计算stri ...

  6. Android 数据库 OrmLite Failed to open database

    04-01 16:49:32.720: E/SQLiteLog(1894): (14) cannot open file at line 30204 of [00bb9c9ce4]04-01 16:4 ...

  7. NIO/AIO

    1 NIO NEW IO 的简称,新的java I/O标准,jdk1.4纳入,有以下特点: (1)NIO基于block块,以块(硬盘数据块)为单位处理数据,比旧式的io基于流的效率高. (2)为各个基 ...

  8. 使用Javascript Ajax 通信操作JSON数据 [上]

    以前只是知道json的格式而已,也做过的是从数据库获得数据然后弄成json的格式然后赋给HighCharts生成曲线,先把数据库的数据使用array()函数转换成数组,然后使用json_encode( ...

  9. ZROI2018普转提day6t3

    传送门 分析 居然卡哈希数,万恶的出题人...... 感觉我这个方法似乎比较呆,我的代码成功成为了全网最慢的代码qwq 应该是可以直接哈希的 但由于我哈希学的不好又想练练线段树维护哈希,于是就写了个线 ...

  10. Entity Framework Tutorial Basics(7):DBContext

    DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...