Count the string HDU - 3336
题意:
求一个字符串的每个前缀在这个字符串中出现次数的加和
解析:
默默的骂一句。。。傻xkmp。。博主心里气愤。。。
拓展kmp就好多了。。。
因为拓展kmp每匹配一次 就相当于这些前缀出现了一次
如abcabc
abcabc 与 abcabc匹配 为6 这个6就相当于 abcabc abcab abca abc ab a各出现了一次
abcabc 与 bcabc 匹配 0
····
abcabc 与 abc 匹配 为3 相当于abc ab a 各出现了一次
明白了吧。。
kmp还用dp 原谅我没看懂 dp蒟蒻。。。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-;
const int maxn = 2e5+;
const int mod = 1e9+; int T;
int n;
char s[maxn];
int dp[maxn]; void solve()
{
int ans = ;
memset(dp,,sizeof(dp));
int pos = ,i = ;
while(i+<n&&s[i]==s[i+]) i++;
dp[] = i;
dp[] = n;
for(int i = ;i < n;i++){
if(dp[i-pos]+i<dp[pos]+pos){
dp[i] = dp[i-pos];
}
else{
int j = dp[pos]+pos-i;
if(j<) j = ;
while(i+j<n&&s[j]==s[i+j]){j++;}
dp[i] = j;
pos = i;
}
}
for(int i = ;i < n;i++){
ans+=dp[i];
}
cout<<ans%<<endl;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
scanf("%s",s);
solve();
}
return ;
}
Count the string HDU - 3336的更多相关文章
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- Count the string - HDU 3336(next+dp)
题目大意:给你一个串求出来这个串所有的前缀串并且与前缀串相等的数量,比如: ababa 前缀串{"a", "ab", "aba", &quo ...
- Count the string -- HDOJ 3336
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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(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) ...
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- linux 命令缩写
su super user apt advanced packaging tool ifconfig interface configuration so shared object fsp frac ...
- 拼多多商品id怎么查看 拼多多店铺ID怎样看
网上开店平台有很多编号.id等可以区分商品和店铺的标志,拼多多有店铺id也有商品id,这是两个不同的概念,店铺id进入到拼多多店铺即可查询,拼多多商品id怎么查看 拼多多店铺ID怎样看,那么拼多多商品 ...
- Jmeter资源监控工具ServerAgent运行原理的一些研究
用过Jmeter的应该都了解,有个ServerAgent工具,放在linux或者windows服务器上开启服务后,在Jmeter中配置下监视器,就可以抓取到服务器的一些资源信息,抓取的主要是cpu.内 ...
- Siki_Unity_3-8_Lua编程(未完)
Unity 3-8 Lua编程 任务1&2&3:前言 课程内容: Lua从入门到掌握 为之后的xLua和其他热更新方案打下基础 任务4:Lua简介 Lua是轻量小巧的脚本语言--无需编 ...
- mysql 数据库优化之执行计划(explain)简析
数据库优化是一个比较宽泛的概念,涵盖范围较广.大的层面涉及分布式主从.分库.分表等:小的层面包括连接池使用.复杂查询与简单查询的选择及是否在应用中做数据整合等:具体到sql语句执行效率则需调整相应查询 ...
- 【python 3.6】python读取json数据存入MySQL(一)
整体思路: 1,读取json文件 2,将数据格式化为dict,取出key,创建数据库表头 3,取出dict的value,组装成sql语句,循环执行 4,执行SQL语句 #python 3.6 # -* ...
- pager-taglib2.0中文传参乱码问题
1.重现问题 在web项目中有时会用到pager-taglib来作为分页的标签,如上图红色框标识所示,当我们需要把页面参数保持的时候我们会在<pg:param />标签中把参数进行传递. ...
- python---json.dumps 与 json.loads /json.dump 和json.load区别
json.dumps 是将python的数据类型进行json的编码,生成json格式的数据,举例json_data = json.dumps(str) str为python的字符串类型数据,生成的j ...
- 20个常用Linux性能监控工具/命令
20个常用Linux性能监控工具/命令 对于 Linux/Unix 系统管理员非常有用的并且最常用的20个命令行系统监视工具.这些命令可以在所有版本的 Linux 下使用去监控和查找系统性能的实际原因 ...
- Scrum7
冲刺阶段的总结 一.各个成员今日完成的任务 组员 任务分工 贡献 林泽宇 团队分工.撰写博客.修改完善需求规格说明书.整理代码规范 李涵 后端架构设计 尹海川 logo设计修改.数据库数据 郏敏杰 课 ...