惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
博客园_首页
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
宝玉的分享
宝玉的分享
量子位
U
Unit 42
D
Docker
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Azure Blog
Microsoft Azure Blog
C
CERT Recently Published Vulnerability Notes
MyScale Blog
MyScale Blog
博客园 - 叶小钗
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
Securelist
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity
H
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
Latest news
Latest news
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
T
The Blog of Author Tim Ferriss
P
Privacy International News Feed
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
F
Full Disclosure
Recent Announcements
Recent Announcements
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
V
Vulnerabilities – Threatpost
腾讯CDC

Makerlife 的小站

2025-2026 赛季 游记 && 退役记 NOIP 2024 游记 集训记录 CSP2024 游记 板子库 动态规划 刷题记录 杂题乱记 数学期望 学习笔记 数论 学习笔记 CF1000F One Occurrence 题解 CSP2023 游寄 主定理 CF1695C Zero Path 题解 字符串算法全家桶 学习笔记 AT_ABC306D 题解 2023.06.03 模拟赛 Azure for Students 使用指北 AT_ABC286C 题解 洛谷 AT1898 题解 洛谷 CF1036A 题解 洛谷 CF1040A 题解 洛谷 SP3591 题解 洛谷 AT278 题解 洛谷 CF141B 题解 洛谷 AT2561 题解 洛谷 AT3525 题解 洛谷 CF899B 题解 洛谷 AT4787 题解 洛谷 AT4810 题解 洛谷 SP5450 题解
P6878 [JOI 2020 Final] JJOOII 2 题解
Makerlife · 2023-10-28 · via Makerlife 的小站

Problem Link

Explanation

给定一个只包含 J\tt JO\tt OI\tt I 三种字符、长度为 NN 的字符串 SS 和一个正整数 KK。定义 KKJOI\tt JOI 串为由恰好 KKJ\tt JKKO\tt OKKI\tt I 依次拼接而成的字串。如 22JOI\tt JOI 串为 JJOOII\tt JJOOII。你可以对 SS 进行任意次以下操作以将 SS 变为 KKJOI\tt JOI 串:

  1. 删除 SS 的第一个字符
  2. 删除 SS 的最后一个字符
  3. 删除 SS 的任意一个字符

要求最小化并输出第三种操作的次数。如果不能将 SS 变为 KKJOI\tt JOI 串,输出 -1

Solution

可以发现只要定位到了最前端的 J\tt J 的位置,那么就可以确定一个最短的 JOI\tt JOI 串。

即我们可以暴力从前向后扫 J\tt J 的位置,然后依次找到 KKO\tt OI\tt I 即可。

可以对上面的算法进行优化,我们记录每个 J\tt JO\tt OI\tt I 的位置为 cjcoci,那么一段 J\tt J 的开始位置即为 cjjcj_j,结束位置为 cjj+k−1cj_{j+k-1}O\tt OI\tt I 同理。

Core Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int n,k;
char s[N];
int cj[N],co[N],ci[N];
int totj,toto,toti;
int ans=INF;
int main()
{
n=read();k=read();
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
if(s[i]=='J') cj[++totj]=i;
if(s[i]=='O') co[++toto]=i;
if(s[i]=='I') ci[++toti]=i;
}
for(int i=1;i<=totj;i++)
{
if(i+k-1>totj) break;//后面不足 k 个 j,下面 o,i 同理
int ed=cj[i+k-1];//一段 j 的结束,下面 o,i 同理
int pos=1;
while(co[pos]<=ed && pos<=toto) pos++;//o 的起始位置,下面 i 同理
if(pos+k-1>toto) break;
ed=co[pos+k-1];
pos=1;
while(ci[pos]<=ed && pos<=toti) pos++;
if(pos+k-1>toti) break;
ed=ci[pos+k-1];
ans=min(ans,ed-cj[i]+1-3*k);//答案为枚举的区间长度与 3*k 的差
}
printf("%d\n",(ans==INF)?-1:ans);
return 0;
}