当前位置:网站首页>洛谷P3426 [POI2005]SZA-Template 题解
洛谷P3426 [POI2005]SZA-Template 题解
2022-06-26 12:32:00 【q779】
洛谷P3426 [POI2005]SZA-Template 题解
题目链接:P3426 [POI2005]SZA-Template
题意:你打算在纸上印一串字母。
为了完成这项工作,你决定刻一个印章。印章每使用一次,就会将印章上的所有字母印到纸上。
同一个位置的相同字符可以印多次。例如:用
aba这个印章可以完成印制ababa的工作(中间的a被印了两次)。但是,因为印上去的东西不能被抹掉,在同一位置上印不同字符是不允许的。例如:用aba这个印章不可以完成印制abcba的工作。因为刻印章是一个不太容易的工作,你希望印章的字符串长度尽可能小。
输入一个长度不超过 5 × 1 0 5 5 \times 10^5 5×105 的非空字符串(只包含小写字母),代表要在纸上印的字符。
因为印章可以随便印,也就是无所谓什么顺序和数量
所以我们可以考虑什么样的情况可以继续
观察样例
ababbababbabababbabababbababbaba
ababbaba
ababbaba
ababbaba
ababbaba
ababbaba
最前面的ababbababbaba十分有趣
可以发现最左侧的一定是要印一次的(废话)
而重复印刷的显然和kmp的border有关
注:对字符串 s s s 和 0 ≤ r < ∣ s ∣ 0 \le r < |s| 0≤r<∣s∣ ,
若 s s s 长度为 r r r 的前缀和长度为 r r r 的后缀相等,就称 s s s 长度为 r r r 的前缀是 s s s 的 border。
摘自oi-wiki
考虑先求出border,然后用dp来推
设 f i f_i fi 表示印刷 s s s 的第 i i i 个前缀的最小印章长度
上界为 f i = i f_i=i fi=i
这里 f i f_i fi 在一定条件下是可以从 fail i \text{fail}_i faili 转移的
f i = { f fail i , ∃ j ≥ i − fail i , f j = f fail i , i , default. f_i = \begin{cases} f_{\text{fail}_i},& \exist\, j \ge i-\text{fail}_i,f_j = f_{\text{fail}_i}, \\\\i,&\text{default.} \end{cases} fi=⎩⎪⎨⎪⎧ffaili,i,∃j≥i−faili,fj=ffaili,default.
第一个看上去复杂,
其实就是 ababbababbaba这样border有重叠的情况
用个桶维护即可,然后转移一下就好了
时间复杂度 O ( n ) O(n) O(n)
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(5e5+15)
char s[N];
int n,fail[N],f[N],h[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> (s+1); n=strlen(s+1);
for(int i=2,j=0; i<=n; i++)
{
while(j&&s[i]!=s[j+1])j=fail[j];
if(s[i]==s[j+1])++j;
fail[i]=j;
}
f[1]=1;h[1]=1;
for(int i=2; i<=n; i++)
{
f[i]=i;
if(h[f[fail[i]]]>=i-fail[i])
f[i]=f[fail[i]];
h[f[i]]=i;
}
cout << f[n] << '\n';
return 0;
}
转载请说明出处
边栏推荐
- UDP protocol details [easy to understand]
- Examples of how laravel uses with preload (eager to load) and nested query
- SQL injection in Pikachu shooting range
- HUST網絡攻防實踐|6_物聯網設備固件安全實驗|實驗二 基於 MPU 的物聯網設備攻擊緩解技術
- Microservice governance (nocas)
- 7-2 大盗阿福
- 一个初级多线程服务器模型
- Ctfshow web getting started command execution web75-77
- Seven major trends deeply affecting the U.S. consumer goods industry in 2022
- Five strategies and suggestions of member marketing in consumer goods industry
猜你喜欢

2、 MySQL Foundation

Ctfshow web getting started command execution web75-77

AD - 将修改后的 PCB 封装更新到当前 PCB 中

Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)

Fengshentai old shooting range Kali series

Scala-day02- variables and data types

Jmeter响应时间和tps监听器使用教程

2021 q3-q4 investigation report on the use status of kotlin multiplatform

PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)

International beauty industry giants bet on China
随机推荐
Is it safe to open a securities account
Rookie practical UML - activity diagram
大智慧哪个开户更安全,更好点
redis通过6379端口无法连接服务器
How can we reach members more effectively?
Polarismesh series articles - concept series (I)
Build document editor based on slate
fastjson的JSONArray和JSONObject[通俗易懂]
A most practical arbitrage wizard EA [2022 modified version]
Scala-day03- operators and loop control
Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
Laravel subdomain accesses different routing files and different modules
Operation analysis and investment prospect research report of China's organic chemical raw material manufacturing industry 2022-2028
Build Pikachu shooting range and introduction
Hello! Forward proxy!
女性科学家的流失
Generate JDE dot train
Ubuntu安装配置PostgreSQL(18.04)
Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
Deep thinking from senior member managers