hdu 3336:Count the string(数据结构,串,KMP算法)
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
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.
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.
#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算法)的更多相关文章
- 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) ...
- 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 ...
- hdu 3336 Count the string(思维可水过,KMP)
题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ACM hdu 3336 Count the string
[题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...
- 基础数据结构-串-KMP算法
KMP算法用于模式串字符匹配,因为没有提前预习,上课时听得云里雾里,后来回去看了一晚上,翻了一些网上的讲解才理解了.我简单讲一下,我们在一串字符串A里搜索匹配另一段字符串B时,思路最简单方法的就是从第 ...
- 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 ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
随机推荐
- Reusing dialogs with a dialog pool--一个sql server service broker例子
一个sql server service broker例子 ----------------------------------- USE master GO -------------------- ...
- STDIN_FILENO vs stdin
数据类型不一致:stdin类型为 FILE*STDIN_FILENO类型为 int 使用stdin的函数主要有:fread.fwrite.fclose等,基本上都以f开头使用STDIN_FILENO的 ...
- Direct X和OpenGL是什么及有何区别
摘自:http://mtoou.info/directx-opengl-shenme/ 提起图形和显卡,尤其是玩电脑游戏的时候(通常是安装时)很多人是不是立刻就想起了一个名词叫做Direct X,通常 ...
- DP SRM 661 Div2 Hard: ColorfulLineGraphsDiv2
Problem Statement Bob is going to create a graph with N nodes. The graph will be constructed in two ...
- iOS开发:icon和启动图尺寸(转)
转至:http://www.cnblogs.com/shaoting/p/4941634.html 歪果仁的总结: Asset iPhone 6s Plus and iPhone 6 Plus (@3 ...
- CString中Format函数与格式输入与输出
CString中Format函数与格式输入与输出 Format是一个非经常常使用.却又似乎非常烦的方法,下面是它的完整概貌.以供大家查询之用: 格式化字符串forma("%d" ...
- SIFT 、Hog 、LBP 了解
SIFT.HOG.LBP,这三者都属于局部特征. 一.三者原理上的区别 1.SIFT:Scale-Invariant Feature Taransform,尺度不变特征变换. 尺度空间的极值检测:搜索 ...
- MockServer 入门
忽略元数据末回到原数据开始处 MockServer介绍及文档 借鉴公司的文档 http://mock-server.com github:https://github.com/jamesdbloom/ ...
- unity, standard shader消耗两个draw call
假设场景中只放一个球,关掉阴影和skybox,球体使用Unlit/Texture shader,则draw call数为2(背景占一个draw call,球占一个draw call). 相同情况下若将 ...
- Altera Quartus II下载中途失败
1.Altera 的quartus II 下载程序时中途失败,有可能是程序写的不对(缺少初始状态,else补全等等) 2.下载程序pof最好也用英文命名,再下载.