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

  可以确定肯定不能拿每个前缀跑去KMP,那么来回想一下fail(next)数组的意义——最长相同的前缀和后缀。对于长度为i的前缀,那么f[i]中包含两个长度为f[i]的前缀(大概就这个意思),用cnt[i]表示1-i这一段中有多少个子串与长度为f[i],f[f[i]]的前缀相同(除去cnt[f[i]]这一部分),于是可以得出

  cnt[i] = cnt[f[i]] + 1

  最后把所有cnt求个和就是最终答案

Code

 /**
* hdu
* Problem#3336
* Accepted
* Time:46ms
* Memory:3316k
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<ctime>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} int kase;
int n;
char s[];
int f[];
int cnt[]; inline void init() {
readInteger(n);
gets(s);
gets(s);
} inline void solve() {
f[] = f[] = ;
int j;
for(int i = ; i < n; i++) {
j = f[i];
while(j > && s[i] != s[j]) j = f[j];
f[i + ] = (s[i] == s[j]) ? (j + ) : ();
}
int res = ;
for(int i = ; i <= n; i++)
(cnt[i] = cnt[f[i]] + ) %= , (res += cnt[i]) %= ;
printf("%d\n", res);
} int main() {
readInteger(kase);
while(kase--) {
init();
solve();
}
return ;
}

hdu 3336 Count the string -KMP&dp的更多相关文章

  1. hdu 3336 Count the string KMP+DP优化

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

  2. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  3. HDU 3336 Count the string ( KMP next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

  4. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  5. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

随机推荐

  1. Oracle体系结构之参数文件管理

    参数文件作用:主要用来记录数据库配置信息,数据库在启动时,需要读取参数文件中关于控制文件的信息,分配内存,打开进程,会话等.数据库启动时第一个读取参数文件. 参数文件分类: 1)pfile:文本文件, ...

  2. python脚本中为什么要在目录前加一个r

    目的:为了避免转义,r后面的内容,全文输出: r是保持字符串原始值的意思,就是说不对其中的符号进行转义.因为windows下的目录字符串中通常有斜杠"\",比如,windows下的 ...

  3. CentOS配置JDK环境

    1 .下载JDK,jdk1.7.0_80.gz 2. mkdir -p /usr/lib/java 3.sudo chmod -R 777 /usr/lib/java 4.tar -zxvf jdk1 ...

  4. 你可能用得到的9段CSS代码

    一.opacity兼容 .transparent {    filter: alpha(opacity=50);/* internet explorer */    -khtml-opacity: 0 ...

  5. 前端 HTML文档 详解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 010-mac下常用命令

    1.查看某个端口是否运行 lsof -i tcp:port lsof -i:8080 2.强制关闭进程 kill -9 PID

  7. SQL死锁知识及解决办法

    [翻译]:SQL死锁-死锁排除 min.jiang 2014-03-18 00:23 阅读:874 评论:1     项目中死锁的解决经历 min.jiang 2014-03-17 01:09 阅读: ...

  8. android 代码edittext删除或者替换光标处的字串

    https://stackoverflow.com/questions/3609174/android-insert-text-into-edittext-at-current-position Cp ...

  9. zkSNARK 零知识验证

    参考文献 ZCash7篇,有社区翻译版,但还是推荐看原汁原味的     https://z.cash/blog/snark-explain.html Vitalik3篇,小天才作者我就不介绍了,这三篇 ...

  10. 搭建基于HTTP协议内网yum仓库

    目录 1. 前言 2. 把rpm包下载到本地 3. 配置nginx对外提供服务 4. 配置本地repo文件 5. 生成repodata信息 6. 检查及使用 7. 对管理机器上的仓库进行更新 参考资料 ...