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

推荐订阅源

SecWiki News
SecWiki News
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
O
OpenAI News
博客园 - 聂微东
Y
Y Combinator Blog
N
News | PayPal Newsroom
IT之家
IT之家
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
L
LangChain Blog
L
Lohrmann on Cybersecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
C
CERT Recently Published Vulnerability Notes
爱范儿
爱范儿
P
Palo Alto Networks Blog
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
G
Google Developers Blog
PCI Perspectives
PCI Perspectives
The Register - Security
The Register - Security
H
Heimdal Security Blog
V
Visual Studio Blog
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
TaoSecurity Blog
TaoSecurity Blog
博客园 - Franky
T
The Blog of Author Tim Ferriss
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
The Hacker News
The Hacker News
U
Unit 42
Security Latest
Security Latest
The GitHub Blog
The GitHub Blog

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;
}