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代码: # ...
随机推荐
- 金蝶K3,域环境中,无本地用户管理员权限的域用户如何设置注册表权限?
如果该用户是属于Power Users组:只需要给用户添加注册表中的HKEY_LOCAL_MACHINE的SOFTWARE完全控制的权限.(勾选允许父项的继承权限和传播到该对象和所有子对象) 如果该用 ...
- maven常用的一些依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...
- centos下 安装jdk
JDK 1.7下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html JDK ...
- AFLW如何获取你想要的21点人脸关键点数据
目前人脸检测和人脸的关键点的数据库根据关键点个数:5,20,21,29,68等.https://blog.csdn.net/XZZPPP/article/details/74939823该网页详细列出 ...
- Timer Schedule参数说明
Timer是一种定时器工具,用来在一个后台线程计划执行指定任务.它可以计划执行一个任务一次或反复多次. TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务. schedule的意 ...
- unity, 用脚本创建mesh
创建一个空gameObject,添加Mesh Filter和Mesh Renderer两个component,再添加一个脚本createMeshScript: using UnityEngine;us ...
- 安卓-APP安装后多个图标的解决
原因是在不同Activity的intent中配置了多个LAUNCHER. <intent-filter> <action android:name="android.int ...
- Atitit.软件仪表盘(7)--温度监测子系统--电脑重要部件温度与监控and警报
Atitit.软件仪表盘(7)--温度监测子系统--电脑重要部件温度与监控and警报 Cpu温度,风扇转速 主板温度 显卡温度 硬盘温度 电池温度 鲁大师 硬盘温度 Cpu温度 core temp ...
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...
- Vs code 通用插件
Vs code 通用插件 转自:https://segmentfault.com/a/1190000006697219 HTML Snippets 超级实用且初级的 H5代码片段以及提示 HTML C ...