Count the string

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1711 1686 3746 1358 3341 

 
  数据结构:串:KMP算法。
  没怎么看明白KMP算法的实现,看别人的代码也似懂非懂,总之最后参考着改还是AC了。
  KMP算法分两步:1、产生next[]数组。2、KMP处理。
  这道题只用到第一步,在生成next[]的函数中做些改动,即可通过它获得结果。
  没搞透,有时间再看看。
 
 #include <iostream>
#include <string.h>
using namespace std;
char s[];
int next[];
int c[];
int ans;
void GetNext(char t[],int next[])
{
memset(c,,sizeof(c));
int j,k;
j=;k=-;next[] = -;
int length;
for(length = ;t[length]!='\0';length++);
while(j<length){
if(k==- || t[j]==t[k]){
if(k!=-){
c[j] = c[k] + ;
ans+=c[j];
}
j++;k++;
next[j] = k;
}
else
k = next[k];
}
}
int main()
{
int T,n;
cin>>T;
while(T--){
cin>>n;
cin>>s;
ans = ;
GetNext(s,next);
cout<<(ans+n)%<<endl;
}
return ;
}

  重又做了一遍这道题,这次大体明白了KMP算法是怎么回事,但是这道题的DP部分还是不太懂。

  DP公式为 dp[j]=dp[next[j]]+1; 代表以前j个字母为前缀的字符串在总字符串中出现的次数-1。

  代码:

 #include <iostream>

 using namespace std;
char a[];
int next[];
int dp[];
int n,ans;
void GetNext(void) //KMP算法自己写的
{
int j=,k=-;
next[] = -;
while(j<n){
if(k==-){
j++;k++;
next[j] = ;
}
else if(a[j]==a[k]){
dp[j] = dp[k] + ; //DP公式
ans += dp[j]; //累加次数
next[j+] = k + ;
j++;
k = next[j];
}
else
k = next[k];
}
}
int main()
{
int T;
cin>>T;
while(T--){
cin>>n;
cin>>a;
ans = ;
GetNext();
cout<<(ans+n)%<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 3336:Count the string(数据结构,串,KMP算法)的更多相关文章

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

  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)

    题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...

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

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

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

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

  6. ACM hdu 3336 Count the string

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

  7. 基础数据结构-串-KMP算法

    KMP算法用于模式串字符匹配,因为没有提前预习,上课时听得云里雾里,后来回去看了一晚上,翻了一些网上的讲解才理解了.我简单讲一下,我们在一串字符串A里搜索匹配另一段字符串B时,思路最简单方法的就是从第 ...

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

  9. HDU 3336 Count the string KMP

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

随机推荐

  1. Creating a Fragment: constructor vs newInstance()

    from stack overflow and another chapter I recently grew tired of constantly having to know String ke ...

  2. 〖Linux〗proc的一些文件读取操作

    /* * ===================================================================================== * * Filen ...

  3. Android 设计的几处硬伤

    [核心提示] 一些 Android App 不仅仅是设计风格的问题,产品交互上也比较混乱,造成用户体验不一致,这一部分原因也是 Android 当初设计时遗留的问题. 前几天看到 NovaDNG 介绍 ...

  4. NuGet学习笔记(2) 使用图形化界面打包自己的类库[转]

    http://www.cnblogs.com/lzrabbit/archive/2012/05/01/2477607.html 上文NuGet学习笔记(1) 初识NuGet及快速安装使用说到NuGet ...

  5. C++:SQLServer字段赋值

    大前提: 1. 初始化环境 2. 创建连接实例 3. 创建记录集实例 注意点: 1.AddNews失败问题: (1)是否将CursorTypeEnum cursorType, LockTypeEnum ...

  6. Python根据系统环境配置日志,Python配置日志 Python logger

    我们通常在写爬虫的时候,需要配置日志,但是有可能是在windows开发的,但是程序的运行环境可是是在Linux中,这时候我们就需要不停的更换日志的目录了 但是我们可以实现通过判断不同的运行环境,来时间 ...

  7. ubuntu下载linux内核源码

    ubuntu仓库里面关于源码部分配置的好全啊,什么都有,ps:包括vim的各种插件居然也有人打包放到仓库里,真是方便. 1.首先查看一下本系统使用的内核版本号: cat /proc/version L ...

  8. CYQ聊天遇到的问题

    action.Data["yj_id"].Value  用action.Get<int>("yj_id"); 这种写法安全 如果是代码里怎么判断,a ...

  9. Object-C支持多继承吗?可以实现多个接口吗?Category是什么?

    转自:http://blog.sina.com.cn/s/blog_7afd7d7801016t3t.html Object-C支持多继承吗?可以实现多个接口吗?Category是什么?重写一个类的方 ...

  10. Eclipse中导入JDK类库的源代码以及添加指定的API

    一.在Eclipse中导入JDK类库的源代码 操作步骤: 打开eclipse->“window”-> “Preferences” -> “Java” -> “Installed ...