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) ...
随机推荐
- .net mvc 使用ueditor的开发(官网没有net版本?)
1.ueditor的下载导入 官网下载地址:https://ueditor.baidu.com/website/download.html · 介绍 有两种,一种开发版,一种Mini版,分别长这样: ...
- Django——多网页网站及网页互联
在helloapp文件夹下添加名为templates的文件夹(此文件夹名称是固定的),并在其下添加html文件,文件内容根据自己网页想呈现的内容而定 在views文件内添加新的函数 在urls文件内添 ...
- sql 命令使用简单记录
半个月前就想记下用过的SQL命令的!!! 主题: 按时间查询: https://blog.csdn.net/hejpyes/article/details/41863349 左关联: se ...
- elasticsearch备份与恢复
备注:以下代码在kibana插件下运行: # 创建一个备份用的仓库# type:fs文件系统# 支持Shared filesystem, Amazon S3, HDFS和Azure #Cloud# l ...
- Hyperledger Fabric CouchDB as the State Database——使用CouchDB
使用CouchDB作为状态数据库 状态数据库选项 状态数据库包括LevelDB和CouchDB.LevelDB是嵌入在peer进程中的默认键/值状态数据库,CouchDB是一个可选的外部状态数据库.与 ...
- 第二次ScrumMeeting博客
第二次ScrumMeeting博客 本次会议于10月26日(四)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.方科栋.辛德泰.窦鑫泽.张安澜. 1. 每个人的工作(有Issue的内容 ...
- 大数据-storm学习资料视频
storm学习资料视频 https://pan.baidu.com/s/18iQPoVFNHF1NCRBhXsMcWQ
- 对其中的一个特点将NABC的分析结果
一.题目要求 每一个组员针对其中的一个特点将NABC的分析结果发表博客上(截止日期4月8日晚24:00前). 二.分析结果 特点之一:通讯方便 <渴了么>这个安卓APP特点之一就是通讯方便 ...
- DPDK报文分类与访问控制
原创翻译,转载请注明出处. dpdk提供了一个访问控制库,提供了基于一系列分类规则对接收到的报文进行分类的能力.ACL库用来在一系列规则上执行N元组查找,可以实现多个分类和对每个分类查找最佳匹配(最高 ...
- android入门 — Activity启动模式
1.standard模式 standard模式是系统的默认启动方式,每次激活Activity都会创建Activity,并放在任务栈中. 系统不会在乎活动是否已经存在于返回栈中,每次启动都会创建该活动的 ...