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

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

Makerlife 的小站

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

洛谷题目传送门 | AT 原题传送门

思路

观察题目可以发现 A 操作最多只能执行 nn 次,超过以后字符串又会回到初始状态。

首先考虑 A 操作如何实现,一种办法是将 SS 在原串后复制一遍,通过移动一个记录初始位置的指针(本文中为 ii)来实现截取 nn 位字符。每次移动指针代价都为 AA

接下来考虑 B 操作的代价计算。我们可以判断之前截取的字符串是否为回文。回文字符串判断应该都会吧通过移动起始位置和结束位置指针,比较两指针对应的字符是否相同进行回文判断,每次遇到不同,总代价都加 BB

值得注意的是结束位置指针的取值。每次 A 操作执行完成,截取出的字符串末位下标为 n+i−1n+i-1,在此基础上再减去起始位置即可。

回文判断部分代码:

1
2
3
4
5
6
7
8
9
10
/*i 为 A 操作中起始位置指针,t 为总代价*/
for(int j=0;j<n/2;j++)//回文判断循环变量只需到 n/2
{
int x=i+j;//起始位置
int y=n+i-1-j;//结束位置
if(s[x]!=s[y])
{
t+=b;
}
}

以 A 操作中起始位置指针作为外层循环变量,范围 0∼n−10\sim n-1,内层循环为回文串判断。总时间复杂度 O(n2)O(n^2)

一些小细节

  • 观察数据范围可知需使用 long long\text{long long}

  • 最大值可取值 2622^{62}1ll<<62

完整代码

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
#include<cstdio>
#include<string>
#include<iostream>
#define INF 1ll<<62
#define ll long long
#define int ll
using namespace std;
int n,a,b;
int ans=INF;
string s;
signed main()
{
scanf("%lld%lld%lld",&n,&a,&b);
cin>>s;
s+=s;
for(int i=0;i<n;i++)
{
int t=a*i;
for(int j=0;j<n/2;j++)
{
int x=i+j;
int y=n+i-1-j;
if(s[x]!=s[y])
{
t+=b;
}
}
ans=min(ans,t);
}
printf("%lld\n",ans);
return 0;
}