Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8845    Accepted Submission(s): 4104

Problem Description
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
 
Author
foreverlin@HNU
 
Source

题目链接:HDU 3336

比较有意思的一题,因为Next[i]代表了字符串S[1~i](是1~i而不是i~len)中最长公共前后缀的长度同时也是最长公共前缀的结束位置,假设当前循环的到了第K个,那可以发现S[1~k]的前缀S[1~Next[k] ]与S[ (len-Next[k]+1)~K]是一样的。

用cnt[i]表示以i结尾的前缀中所出现的所有重复数就有cnt[i]=cnt[Next[k] ]+1即前面重复过的次数加上此时的又出现一次重复

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=200010;
const int mod=10007;
char s[N];
int nxt[N];
int len;
int dp[N];
void getnext()
{
CLR(nxt,0);
int j=0,k=nxt[0]=-1;
len=strlen(s);
while (j<len)
{
if(k==-1||s[j]==s[k])
nxt[++j]=++k;
else
k=nxt[k];
}
}
int main(void)
{
int tcase,i,j,n,ans;
scanf("%d",&tcase);
while (tcase--)
{
ans=0;
CLR(dp,0);
scanf("%d%s",&n,s);
getnext();
for (i=1; i<=n; ++i)
{
dp[i]=dp[nxt[i]]+1;
ans=(ans+dp[i])%mod;
}
printf("%d\n",ans);
}
return 0;
}

HDU 3336 Count the string(KMP的Next数组应用+DP)的更多相关文章

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

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

  3. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  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][DP]

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

  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 查找匹配字符串

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

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

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

  9. ACM hdu 3336 Count the string

    [题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...

随机推荐

  1. Deeplink做不出效果,那是你不会玩!

    最近魔Sir听一些有些同学说之前用过Deeplink,但效果并没有想象中的那么好,在了解了小伙伴的使用场景后,魔Sir觉得有必要出这么篇东西告诉大家,Deeplink效果差,那是因为你不会玩! 对于已 ...

  2. [Android Pro] PackageManager#getPackageSizeInfo (hide)

    referce to : http://www.baidufe.com/item/8786bc2e95a042320bef.html 计算Android App所占用d的手机内存(RAM)大小.App ...

  3. linux常见问题集锦-1

    http://www.cnblogs.com/itech/archive/2011/02/12/1952857.html 感谢作者分享 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 . ...

  4. p235习题2

    List  成功添加 Set  添加失败

  5. android 兼容性测试 CTS 测试过程(实践测试验证通过)

    source: http://blog.csdn.net/jianguo_liao19840726/article/details/7222814 写这个博客的时候是为了记忆,建议大家还是看官方的说明 ...

  6. Java Hour 56 Spring 和 Hibernate 的集成

    上一章节我们完成了一个简单的Spring 的试验品,这章要让Spring 上战场了,不要慌,步骤都是一样的. Spring 对 Hibernate 的支持是很多方面的,第一个战场是SessionFac ...

  7. [译]ASP.NET 性能监控

    概述:在性能测试中性能监视器是非常强大的工具,本文主要简述了几个关键的计数器和相关的阈值. 监控性能计数器 很多的性能计数器在性能调优中是非常重要的,由于性能计数器太多,很多人不知道选择那些计数器. ...

  8. wp8 --未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出

    今天打算用VisualStudio2012做一个js效果页面测试的时候,打开VS2012新建项目,但是并没有像之前那样顺利的创建页面,而是弹出了一个错误窗口. 我的系统是win8专业版 64位 ,同时 ...

  9. Laravel 4 系列入门教程(一)

    默认条件 本文默认你已经有配置完善的PHP+MySQL运行环境,懂得PHP网站运行的基础知识.跟随本教程走完一遍,你将会得到一个基础的包含登录的简单blog系统,并将学会如何使用一些强大的Larave ...

  10. 【vijos1659】河蟹王国 线段树<区间修改+区间最大值>

    描述 河蟹王国有一位河蟹国王,他的名字叫羊驼.河蟹王国富饶安定,人们和谐相处.有一天,羊驼国王心血来潮,想在一部分人中挑出最和谐的人.于是,羊驼国王将他的子民排成了一列(==!!b汗~好长呀).每个人 ...