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. 洛谷P2679 子串 [noip2015] dp

    正解:dp 解题报告: 感觉是道dp好题啊,所以就写了个题解 代码实现难度低,思维难度大,像我这种思维僵化傻逼选手只想到了爆搜+组合数学... 其实是道很妙的dp题!好趴也没有多妙主要大概是妙在想到了 ...

  2. Loadrunner之https协议录制回放报错如何解决?(九)

    一.录制中遇到报错27778的问题(如下图1),即关于录制的链接为https开头的问题,分两个步骤解决,如下: 图1 https访问报错解决步骤如下: 1.修改Vuser-->Run-time ...

  3. OC 手势可能出现的问题

    oc手势有分別是 Tap(点一下).Pinch(二指往內或往外拨动).Rotation(旋转).Swipe(滑动,快速移动).Pan (拖移,慢速移动)以及 LongPress(长按). UITapG ...

  4. 通过Tacker将NFV引入OpenStack

    14年的这个时候,我们还在OpenStack社区中为NFV是否属于OpenStack而争论不休.如今这一争议已经被解决了.OpenStack已经成为NFV讨论中的重要部分,正如下面的ETSI MANO ...

  5. 使用PHP创建一个REST API(译)

    最近API在网络领域有些风靡,明确的说是REST的影响力.这实在没什么好惊讶的,因为在任何编程语言中,消费REST API都是非常的容易.构建它也非常的简单,因为本质上你不会用到任何那些已存在很久的H ...

  6. vertx异步编程测试

    vertx是异步编程的框架,性能较高,开发简单.异步编程就是当一个请求来了,vertx将其交由一个事件进行处理,然后继续向下执行,等处理完成,返回结果,通知客户端.这是一个由服务端反向调用客户端的过程 ...

  7. shell脚本变量$#,$*,$$,$@,$0,$1,$2,$?的含义

    本文转录:https://blog.csdn.net/kejiaming/article/details/51859503 参数说明: $# 是传给脚本的参数个数 $ 是脚本本身的名字 $ 是传递给该 ...

  8. 安卓手机上微信无法打开Https网址的完美解决方案

    1,第三方网站检测网站的SSL证书是否正确的安装 https://www.geocerts.com/ssl-checker,大概率你会看到下边的场景,一个证书链完整的警告,如果想知道我的基础配置是什么 ...

  9. ssh 远程执行命令

    SSH 是 Linux 下进行远程连接的基本工具,但是如果仅仅用它来登录那可是太浪费啦!SSH 命令可是完成远程操作的神器啊,借助它我们可以把很多的远程操作自动化掉!下面就对 SSH 的远程操作功能进 ...

  10. discuz $_G变量

    class.core.php中 global $_G;        $_G = array(            'uid' => 0,            'username' => ...