当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
- [solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
- HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 3 freertos-mpu protection bypass
- 7-3 最低通行费
- PHP laravel+gatewayworker completes im instant messaging and file transfer functions (Chapter 2: explanation of business logic)
- SQL injection
- VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
- Spark-day01- get started quickly
- Is it safe to open a securities account in general
- dried food! Yiwen will show you SD card, TF card and SIM card!
- China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
猜你喜欢

Scala-day02- variables and data types

HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 2 MPU based IOT device attack mitigation technology

The laravel dingo API returns a custom error message
![[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure](/img/d5/db1931596c26090092aaa065103dbb.png)
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
女性科学家的流失
![[graduation season · advanced technology Er] I remember the year after graduation](/img/e7/8e1dafa561217b77a3e3992977a8ec.png)
[graduation season · advanced technology Er] I remember the year after graduation

环形队列php

Examples of how laravel uses with preload (eager to load) and nested query

Scala-day03- operators and loop control
![[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]](/img/13/c2c63333a9e5ac08b339449ea17654.jpg)
[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
随机推荐
11、 Box styles and user interface
PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
This executeQuery (SQL) cannot compile classes for JSP. What is the reason?
Msvcr110 not found DLL, unable to continue code execution Solution for startup
On the use of protostaff [easy to understand]
CG bone animation
汇编语言(7)运算指令
"Pinduoduo and short video speed version", how can I roast!
2022 edition of investment analysis and "fourteenth five year plan" development prospect forecast report of China's switchgear industry
一个初级多线程服务器模型
Omnichannel membership - tmall membership 2: frequently asked questions
4. N queen problem
Lintcode 130 · stacking
China's smart toy market outlook and investment strategy consulting forecast report from 2022 to 2027
I want to know whether flush is a stock market? Is online account opening safe?
Member system + enterprise wechat + applet to help the efficient transformation of private domain
leetcode 715. Range module (hard)
Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
New routing file in laravel framework
Spark-day02-core programming-rdd