ACM hdu 3336 Count the string
【题意概述】
给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和。
【题目分析】
利用kmp算法的next数组 再加上dp
【存在疑惑】
在分析next数组和dp之间的关系,结论是 dp[i] = (dp[next[i]]+1); 搞不懂之间存在的联系
【AC】
#include <bits/stdc++.h>
int n,m,dp[],next[];
char s[];
void getnext() {
int i=,j=-;
next[]=-;
while(i<m) {
if(j==-||s[i]==s[j]) {
++i;++j;
next[i]=j;
} else
j=next[j];
}
}
int main() {
scanf("%d",&n);
while(n--)
{
int sum=;
scanf("%d",&m);
scanf("%s",s);
getnext();
memset(dp,,sizeof(dp));
for(int i=;i<=m;++i) {
dp[i]=(dp[next[i]]+)%;//不理解
sum=(sum+dp[i])%;
}
printf("%d\n",sum);
}
return ;
}
参考博客:https://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html
ACM hdu 3336 Count the string的更多相关文章
- 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
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 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(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 3336——Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
随机推荐
- python/ Django之中间件
python/ Django之中间件 一.中间件 中间件共分为: (1)process_request(self,request) (2)process_view(self, request, cal ...
- python/ORM操作详解
一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...
- windows10无法启动承载网络
每个都试一下
- tornado框架源码分析---Application类之debug参数
先贴上Application这个类的源码. class Application(httputil.HTTPServerConnectionDelegate): """A ...
- Windows下安装Python3和Django
下载python3 首先去 python的官网 下载最新稳定版的python3, 我下载的时候python3的最新版本是3.6.5. 亦可点击 此链接 直接下载. 安装python3 傻瓜式安装,注意 ...
- 【转】JAVA异常报错大全
算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:NegativeAr ...
- geotrellis使用(三十九)COG 写入更新
前言 前面介绍过了如何在 ETL 的时候更新 Layer,使得能够在大数据量的时候完成 ETL 操作,同时前两篇文章也介绍了 COG 以及如何在 Geotrellis 中实现 COG 的读取.本文介绍 ...
- [LeetCode] Employee Importance 员工重要度
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 深入java多线程一
涉及到 1.线程的启动(start) 2.线程的暂停(suspend()和resume()) 3.线程的停止(interrupt与异常停止,interrupt与睡眠中停止,stop(),return) ...
- [SHOI2016]黑暗前的幻想乡
Description 四年一度的幻想乡大选开始了,最近幻想乡最大的问题是很多来历不明的妖 怪涌入了幻想乡,扰乱了幻想乡昔日的秩序.但是幻想乡的建制派妖怪(人类) 博丽灵梦和八云紫等人整日高谈所有妖怪 ...