题意:

  求一个字符串的每个前缀在这个字符串中出现次数的加和

解析:

  默默的骂一句。。。傻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的更多相关文章

  1. (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 ...

  2. Count the string - HDU 3336(next+dp)

    题目大意:给你一个串求出来这个串所有的前缀串并且与前缀串相等的数量,比如: ababa 前缀串{"a", "ab", "aba", &quo ...

  3. Count the string -- HDOJ 3336

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 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) ...

  5. 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 ...

  6. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. centos7下将java -jar命令运行一个项目做成systemd服务

    有些时候运行一个java项目在linux下通过一条简单的java命令即可,如: #nohup java -jar jenkins.war & ###这里为后台运行jenkins 在此背景下,j ...

  2. 【BZOJ3142】[HNOI2013]数列

    [BZOJ3142][HNOI2013]数列 题面 洛谷 bzoj 题解 设第\(i\)天的股价为\(a_i\),记差分数组\(c_i=a_{i+1}-a_i\) 则 \[ Ans=\sum_{c_1 ...

  3. Linux 的字符串截取

    假设有变量 var=http://www.aaa.com/123.htm 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} 其中 var 是变量名,# 号是运算符,*// ...

  4. protobuf工程的编译以及使用

    一. 获取Protocol Buffer 1.1 获得源码 Github:ProtocolBuffer源码 Git clone之:git clone https://github.com/google ...

  5. jmeter逻辑控制器

    刚开始学习,只写几种了解的逻辑控制器 1.简单控制器 只用来组合采样器和其他逻辑控制器,不影响jmeter的运行 2.循环控制器 用来循环执行采样器和其他逻辑控制器,例如一个用户发送特定请求多次,即可 ...

  6. KRKR基础篇(二)

    这里介绍一些krkr的语法规范,具体的命令含义及用法以后再叙述 一:kag语法及基本概念 KAG使用的剧本语言为KAG Script,文件扩展名为.ks 脚本内的文字除  注释,  命令 ,  段落标 ...

  7. Java实现网上商城

    // 第一个JavaWeb项目 //练手项目没有使用框架 github下载 https://github.com/dejavudwh/Online-Shopping 项目截图 1.基本实现了购物网站该 ...

  8. openvpn部署

    原文发表于cu:2016-03-29 参考文档: 安装:http://qicheng0211.blog.51cto.com/3958621/1575273 安装:http://www.ipython. ...

  9. 关于购买Redis服务器:腾讯云、阿里云还是华为云?

    个人分类: redis使用 编辑 新年伊始,很多商家都开始进行新年产品大促销,在分布是缓存Redis领域,几家大公司也是打得如火如荼,各有千秋啊. 现在市场上比较有口碑的商家有腾讯云.阿里云.华为云三 ...

  10. Elasticsearch 统计代码例子

    aggs avg 平均数 最近15分钟的平均访问时间,upstream_time_ms是每次访问时间,单位毫秒 { "query": { "filtered": ...