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. Python: Soft_max 分类器

    我们能够建立例如以下的loss function: Li=−log(pyi)=−log⎛⎝efyi∑jefj⎞⎠ L=1N∑iLi+12λ∑k∑lW2k,l 以下我们推导loss对W,b的偏导数,我们 ...

  2. 转:sock_ev——linux平台socket事件框架(socket代理类) .

    前面分析了对socket基本操作的封装,并按照数据的传送方式写了两个类,本篇将写一个代理类提供给库的使用者使用的类. /**************************************** ...

  3. Android 弹幕效果开发案例

    概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂移移 ...

  4. Ubuntu系统使用命令禁用触摸板等输入设备

    [日期:2012-02-22]   本人用的Ubuntu 10.04系统,笔记本上有鼠标和触摸板.默认下,触摸板是开启的, 有时候打字的时候会不时碰到触摸板,添了不少麻烦,所以得禁用触摸板,限于目前所 ...

  5. EMQ ---websocket

    简介 近年来随着 Web 前端的快速发展,浏览器新特性层出不穷,越来越多的应用可以在浏览器端或通过浏览器渲染引擎实现,Web 应用的即时通信方式 WebSocket 得到了广泛的应用. WebSock ...

  6. Linux下 sleep函数的注意事项

    1. 休眠sleep(unsigned int)为线程内操作  所以如果不同线程,信号量SIGALRM是不能中断sleep():  编写程序进行测试 //timercreate_demo.cpp #i ...

  7. unity5, 在unity中编辑动画

    如图,dock是一个空gameObject,其下包含mouth_dn,mouth_up (应该叫lip_dn,lip_up更合适,这不是重点,先不改了),head,eye_left,eye_right ...

  8. MySQL-Transfer2.3发布

    Transfer 2.3发布,下载地址 此版本除了升级based版本外 *优化了无索引表的同步性能 *优化了slave模式下超大事务内存消耗问题 *Transfer模式相关的功能改动较多 *修复tra ...

  9. Atitit.c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结

    Atitit.c# .net 3.5 4.0 各个版本新特性战略规划总结 1. --------------.Net Framework版本同CLR版本的关系1 2. paip.----------- ...

  10. Atitit. 解释器模式框架选型 and应用场景attilax总结 oao

    Atitit. 解释器模式框架选型 and应用场景attilax总结 oao 1. 解释器模式结构描述 1 2. 如何实现(简单的解释器模式,仅仅通过词法分析即可实现,而无需token流进行处理. 2 ...