题意:

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

解析:

  默默的骂一句。。。傻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. 1109: [POI2007]堆积木Klo

    1109: [POI2007]堆积木Klo https://lydsy.com/JudgeOnline/problem.php?id=1109 分析: 首先是dp,f[i]表示到第i个的最优值,f[i ...

  2. 安装支持elasticsearch使用sql查询插件

    一.ElasticSearch-SQL介绍 ElasticSearch-SQL(后续简称es-sql)是ElasticSearch的一个插件,提供了es 的类sql查询的相关接口.支持绝大多数的sql ...

  3. python全栈开发-前方高能-内置函数2

    python_day_15 一.今日主要内容 1. lambda 匿名函数 语法: lambda 参数:返回值 不能完成复杂的操作 2. sorted() 函数 排序. 1. 可迭代对象 2. key ...

  4. 你的第一个接口测试:Python 接口测试

    前言: 首先我们先明确一个概念,什么叫接口.什么叫接口测试? 接口的全称叫[Application Programming Interface 又叫API],是提供应用程序与开发人员基于某软件或硬件得 ...

  5. realstudio 粒子特效问题总结

    ParticleEmitter._inner_material.flags.depth_write = true;ParticleEmitter._inner_material.flags.depth ...

  6. Update类型_JDBC的方法_JAVA方法_Loadrunner脚本

    java vuser   JDBC 参数化的方法 如果不进行参数化 直接把32  33行去掉 ,sql 值写到valuers  中就行了 下面这是 insert,delete,update  三种方法 ...

  7. Spark之编程模型RDD

    前言:Spark编程模型两个主要抽象,一个是弹性分布式数据集RDD,它是一种特殊集合,支持多种数据源,可支持并行计算,可缓存:另一个是两种共享变量,支持并行计算的广播变量和累加器. 1.RDD介绍 S ...

  8. python打印图形大全(详解)

    ,): shixin=chr() print(shixin) -------------------结果:2) for i in range(0,10): shixin=chr(9679) print ...

  9. Linux 深入理解inode/block/superblock

    基础命令学习目录首页 原文链接:https://blog.csdn.net/Ohmyberry/article/details/80427492 档案系统特性 传统的磁盘与档案系统之应用中,一个分割槽 ...

  10. js操作对象属性值为字符串

    今天在项目开发中遇到一个没遇到过的问题,这个问题是需要对比两个对象a和b,a是一个只有一个属性的对象,b是一个含有多个属性对象,如果b中包含和a一模一样的属性名和值,则把这个一样的属性和值从b中删除了 ...