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. android中RecycleView分页原生代码封装,无任何第三方代

    概述 RecycleView分页加载封装,简单方便,功能齐全 详细 代码下载:http://www.demodashi.com/demo/13283.html 一.场景: 在项目开发中经常使用到列表集 ...

  2. 修改host简化远程访问

    问题描述: 使用本机登陆服务器时,需要经常输入IP地址,在局域网下和非局域网下输入的IP又不一样,十分麻烦,如果可以给IP命名一个简单的名字,岂不是很方便? 解决方法: 修改host文件: vim / ...

  3. 点滴积累【JS】---JS实现仿百度模糊搜索效果

    效果: HTML代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="In ...

  4. atitit。win7 win8 win9 win10 win11 新特性总结与战略规划

    atitit.win7 win8 win9 win10  win11 新特性总结与战略规划 1. win7 1 1.1. 发布时间 2009年10月22日 1 1.2. 稳定性大幅提升,很少蓝屏死机 ...

  5. int、char、long各占多少字节数

    Java基本类型占用的字节数:1字节: byte , boolean2字节: short , char4字节: int , float8字节: long , double 编码与中文:Unicode/ ...

  6. 判断是否是IE浏览器和是否是IE11

    判断是否是IE浏览器用下面这个函数, function isIE() { //ie? 是ie返回true,否则返回false if (!!window.ActiveXObject || "A ...

  7. 示例 Demo 工程和 API 参考链接

    Camera Explorer:有关 Windows Phone8 中有关增强 Camera API 的使用.文章链接 Filter Effects:对拍摄的照片或者图片库中的照片应用 Nokia I ...

  8. iPhone4 降级6.12教程 无须SHSH 不装插件 不睡死[转载] by 轻鸢

    无shsh降级电脑系统,细节操作等其它影响因素较多,不确保每个人都能成功,楼主发帖前刷机几十次均成功.步骤有些繁琐,按照步骤每一步都正确可保证最后不睡死 注意一下,无SHSH降级都是不完美的,开机需要 ...

  9. python操作word

    python教程(百度经验) Python 操作Word(Excel.PPT等通用)   import win32comfrom win32com.client import Dispatch, co ...

  10. Android4.4之后休眠状态下Alarm不准时的问题

    Android4.4及之后休眠状态下Alarm不准时的问题 为了减轻功耗,延长电池使用时间.Android 4.4及之后的版本号採用非精准闹钟机制.以及休眠状态下的wakeup类型的alarm不会实时 ...