Codeforces Round 943 (Div. 3) G1. Division + LCP (easy version) 二分+KMP

Division + LCP (easy version)

题目描述

This is the easy version of the problem. In this version l = r l=r l=r .

You are given a string s s s . For a fixed k k k , consider a division of s s s into exactly k k k continuous substrings w 1 , … , w k w_1,\dots,w_k w1,,wk . Let f k f_k fk be the maximal possible L C P ( w 1 , … , w k ) LCP(w_1,\dots,w_k) LCP(w1,,wk) among all divisions.

L C P ( w 1 , … , w m ) LCP(w_1,\dots,w_m) LCP(w1,,wm) is the length of the Longest Common Prefix of the strings w 1 , … , w m w_1,\dots,w_m w1,,wm .

For example, if s = a b a b a b c a b s=abababcab s=abababcab and k = 4 k=4 k=4 , a possible division is a b a b a b c a b \color{red}{ab}\color{blue}{ab}\color{orange}{abc}\color{green}{ab} abababcab . The L C P ( a b , a b , a b c , a b ) LCP(\color{red}{ab},\color{blue}{ab},\color{orange}{abc},\color{green}{ab}) LCP(ab,ab,abc,ab) is 2 2 2 , since a b ab ab is the Longest Common Prefix of those four strings. Note that each substring consists of a continuous segment of characters and each character belongs to exactly one substring.

Your task is to find f l , f l + 1 , … , f r f_l,f_{l+1},\dots,f_r fl,fl+1,,fr . In this version l = r l=r l=r .

输入格式

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 ) — the number of test cases.

The first line of each test case contains two integers n n n , l l l , r r r ( 1 ≤ l = r ≤ n ≤ 2 ⋅ 1 0 5 1 \le l = r \le n \le 2 \cdot 10^5 1l=rn2105 ) — the length of the string and the given range.

The second line of each test case contains string s s s of length n n n , all characters are lowercase English letters.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105 .

输出格式

For each test case, output r − l + 1 r-l+1 rl+1 values: f l , … , f r f_l,\dots,f_r fl,,fr .

样例 #1

样例输入 #1

7
3 3 3
aba
3 3 3
aaa
7 2 2
abacaba
9 4 4
abababcab
10 1 1
codeforces
9 3 3
abafababa
5 3 3
zpozp

样例输出 #1

0
1
3
2
10
2
0

提示

In the first sample n = k n=k n=k , so the only division of a b a aba aba is a b a \color{red}a\color{blue}b\color{orange}a aba . The answer is zero, because those strings do not have a common prefix.

In the second sample, the only division is a a a \color{red}a\color{blue}a\color{orange}a aaa . Their longest common prefix is one.

原题

CF——传送门

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;int n, Left, Right;
const int MAX = 1e6 + 6;
int Next[MAX];
vector<int> ans;
inline void GetNext(string s, int l) // 获得字符串s的Next数组
{int t;Next[0] = -1;               // 如果在0位置失配则是向下移动一位for (int i = 1; i < l; ++i) // 依次求解后面的Next数组{t = Next[i - 1];while (s[t + 1] != s[i] && t >= 0)t = Next[t];if (s[t + 1] == s[i])Next[i] = t + 1;elseNext[i] = -1;}
}
inline void KMP(string s1, int l1, string s2, int l2)
{GetNext(s2, l2);int i = 0, j = 0;while (j < l1){if (s2[i] == s1[j]) // 当前位匹配成功,继续匹配下一位{++i;++j;if (i == l2) // 完全匹配{ans.push_back(j - l2); // 储存答案i = Next[i - 1] + 1;   // 继续匹配}}else{if (i == 0) // 在首位不匹配j++;elsei = Next[i - 1] + 1;}}
}
bool check(string s, string a, int k)
{KMP(s, s.size(), a, a.size());int num = 0;// 去掉重叠的匹配子串if (ans.size() >= 1){num++;int last = ans[0];for (int i = 1; i < ans.size(); i++){if (ans[i] - last > k - 1){num++;last = ans[i];}}}ans.clear();if (num >= Left)return 1;elsereturn 0;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;cin >> t;while (t--){cin >> n >> Left >> Right;string s;cin >> s;// 二分int l = 0, r = n / Left;while (l < r){int mid = (l + r + 1) / 2; // 这里要 l + r +1 要不然会死循环string a = s.substr(0, mid);if (check(s, a, mid)) // check函数自己写{l = mid; // mid这个位置 满足条件之后 查找 [mid , right]的位置, 所以l移到mid的位置}else{r = mid - 1; // [mid,r] 不满足条件, 所以要移到满足条件的一方, r = mid - 1}}cout << l << '\n';}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/6392.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【全网首出】npm run serve报错 Expression: thread_id_key != 0x7777

总结 困扰了一天&#xff01;&#xff01;&#xff01;一直以为是自己哪里配置错了&#xff0c; 结果最后发现是node.js官方的问题&#xff0c; Node.js v16.x版本的fibers.node被弃用 本文阅读大概&#xff1a;3min #npm run serve时就报错 #找了一天的文章&#xff0c;找不…

# 从浅入深 学习 SpringCloud 微服务架构(八)Sentinel(1)

从浅入深 学习 SpringCloud 微服务架构&#xff08;八&#xff09;Sentinel&#xff08;1&#xff09; 一、sentinel&#xff1a;概述 1、前言 – 服务熔断 Hystrix 的替换方案。 1&#xff09;2018年底 Netflix 官方宣布 Hystrix 已经足够稳定&#xff0c;不再积极开发 Hys…

JVM笔记2--垃圾收集算法

1、如何确认哪些对象“已死” 在上一篇文章中介绍到Java内存运行时的各个区域。其中程序计数器、虚拟机栈、本地方法栈3个区域随着线程而生&#xff0c;随线程而灭&#xff0c;栈中的栈帧随着方法的进入和退出而有条不紊的执行着入栈和出栈操作。每个栈帧中分配多少内存基本上…

组队竞赛和删除公共字符

这里附上两个题目的链接 题目一&#xff1a;删除公共字符_牛客题霸_牛客网 (nowcoder.com) 题目二&#xff1a;组队竞赛_牛客笔试题_牛客网 (nowcoder.com) 第一题 分析&#xff1a; 当我们看见这个题目的时候&#xff0c;可以使用传统的暴力查找方式&#xff0c;如判断第一个…

VsCode | 修改首页启动页 Logo

VsCode | 修改首页启动页 Logo 最终效果&#xff1a; 插件的安装 先安装插件 Custom CSS and JS Loader 插件配置 Ctrl Shift P 输入 打开用户设置&#xff0c;在末尾添加 "vscode_custom_css.imports": [""]下载 Logo 下载 Logo 点我下载 引入…

json转excel

前面有介绍过excel文件转换成json文件的方法&#xff0c;那json文件转excel文件呢&#xff1f;如果json文件里数据格式都是统一的话&#xff0c;那么也比较容易就转。 我们假设json文件中是一个json数组&#xff0c;每条json数据的属性字段都一样&#xff0c;手写一段node.js例…

若依分离版-前端使用echarts组件

1 npm list:显示已安装的模块 该命令用于列出当前项目的所有依赖关系&#xff0c;包括直接依赖和间接依赖。执行 npm list 时&#xff0c;npm 将从当前目录开始&#xff0c;递归地列出所有已安装的模块及其版本信息 npm list 2 npm outdated:用于检查当前项目中的npm包是否有…

亚马逊云科技AWS免费证书-EC2服务器设计(含题库)

亚马逊云AWS官方程序员专属免费证书又来了&#xff01;这次证书是关于AWS EC2实例的设计和搭建&#xff0c;EC2作为AWS服务的核心&#xff0c;是学好AWS的第一步。强推没有任何AWS背景和转码的小伙伴去学&#xff01;学完也能变成AWS开发大神&#xff01; 证书名字叫Getting St…

使用 TensorFlow 和 Keras 构建 U-Net

原文地址&#xff1a;building-a-u-net-with-tensorflow-and-keras 2024 年 4 月 11 日 计算机视觉有几个子学科&#xff0c;图像分割就是其中之一。如果您要分割图像&#xff0c;则需要在像素级别决定图像中可见的内容&#xff08;执行分类时&#xff09;&#xff0c;或者从像…

第Ⅰ章-IV npm yarn pnpm 包管理器

第Ⅰ章-Ⅰ 了解Vue3 创建一个Vue3项目 第Ⅰ章-Ⅱ Vue3自定义创建项目 项目文件详解 第Ⅰ章-III Vite 创建vue3 项目 第Ⅰ章-IV npm yarn pnpm 包管理器 第Ⅰ章-IV npm yarn pnpm 包管理器 简介npm工作原理 yarn工作原理 pnpm工作原理 功能脚本添加依赖移除依赖安装所有依赖查看…

Oracle23ai来了,23爱,全能、超级巨兽...

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 作者&#xff1a;IT邦德 中国DBA联盟(ACDU)成员&#xff0c;10余年DBA工作经验&#xff0c; Oracle、PostgreSQL ACE CSDN博客专家及B站知名UP主&#xff0c;全网粉丝10万 擅长主流Oracle、My…

[图解]关于SysML v2(1)大刀阔斧 对比 伪创新圈子

1 00:00:03,960 --> 00:00:08,270 OMG在2月份&#xff0c;这里写了4月 2 00:00:08,440 --> 00:00:13,530 应该是2月&#xff0c;发布了 3 00:00:13,870 --> 00:00:17,700 SysML v2的 beta 2版本 4 00:00:17,870 --> 00:00:19,780 也是当前最新的版本 5 00:00:2…

关于灰度发布

目录 一 来源 二 运行过程 三 适用范围 一 来源 灰度发布&#xff0c;也叫金丝雀发布&#xff0c;起源是&#xff0c;矿井工人发现&#xff0c;金丝雀对瓦斯气体很敏感&#xff0c;矿工会在下井之前&#xff0c;先放一只金丝雀到井中&#xff0c;如果金丝雀不叫了&#xff…

【DevOps】掌控云端:Google Cloud SDK 快速上手

一、Google Cloud SDK Google Cloud SDK (Software Development Kit) 是一组工具,包括 gcloud、gsutil 和 bq,用于通过命令行或自动化脚本访问和管理 Google Cloud 资源和服务。以下是 Cloud SDK 的详细介绍: 1、gcloud 命令行工具 gcloud 是 Cloud SDK 的核心组件,用于管理…

ES的脑裂现象

目录 0 集群结点的职责1 什么是脑裂现象2 造成脑裂现象的原因2.1 网络问题&#xff08;最常见&#xff09;2.2 主节点负载过大&#xff0c;资源耗尽&#xff0c;别的结点ping不到主节点2.3 主节点JVM内存回收时间过长导致 3 脑裂现象的解决方案3.1 局域网部署3.2 角色分离&…

主成分分析(PCA)学习

概述 主成分分析&#xff08;Principal Component Analysis&#xff0c;PCA&#xff09;是一种常用的数据降维方法&#xff0c;它通过线性变换将原始数据变换为一组各维度线性无关的表示&#xff0c;通常用于提取数据的主要特征分量。PCA 的目标是从原始数据中提取出最重要的特…

python实验一 简单的递归应用

实验一 实验题目 1、兔子繁殖问题(Fibonacci’s Rabbits)。一对兔子从出生后第三个月开始&#xff0c;每月生一对小兔子。小兔子到第三个月又开始生下一代小兔子。假若兔子只生不死&#xff0c;一月份抱来一对刚出生的小兔子&#xff0c;问一年中每个月各有多少只兔子。 &…

[每日AI·0501]GitHub 版 Devin,Transformer的强力挑战者 Mamba,Sora 制作细节与踩坑,OpenAI 记忆功能

AI 资讯 国资委&#xff1a;加快人工智能等新技术与制造全过程、全要素深度融合GitHub版 Devin 上线&#xff0c;会打字就能开发应用&#xff0c;微软 CEO&#xff1a;重新定义 IDE在12个视频理解任务中&#xff0c;Mamba 先打败了 TransformerSora 会颠覆电影制作吗&#xff…

(delphi11最新学习资料) Object Pascal 学习笔记---第11章 ( 接口)

第11章 接口 ​ 与C及其他语言不同&#xff0c;Object Pascal不支持多重继承&#xff0c;这意味着每个类只能有一个单一的基类。 ​ 多重继承的实用性是面向对象编程专家争论的议题之一。Object Pascal中缺少多重继承可以被看做一种劣势&#xff0c;因为您没有C的功能强大&am…

Go实现 - 树莓派自己烧录自己 之 多读卡器同时烧录

简介 Go实现 监控读卡器设备存储空间变化&#xff0c; 自动烧写SD Card&#xff0c; 烧写完成之后自动弹出&#xff0c; 显示执行状态&#xff0c; 还支持热插拔。 步骤 代码 lsblkParser.go imageWriter.go package actionimport ("fmt""os/exec" )ty…