当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
- NFS shared storage service installation
- leetcode 715. Range module (hard)
- PHP generate order number
- Omni channel member link - tmall member link 3: preparation of member operation content
- PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
- 【概率论】条件概率、贝叶斯公式、相关系数、中心极限定理、参数估计、假设检验
- Laravel uses find_ IN_ The set() native MySQL statement accurately queries whether a special string exists in the specified string to solve the problem that like cannot be accurately matched. (resolve
- [solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
- AD - 将修改后的 PCB 封装更新到当前 PCB 中
- Leetcode 78. Subset and 90 Subset II
猜你喜欢

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

2、 MySQL Foundation

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

Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files

International beauty industry giants bet on China

1、 MySQL introduction

Vscode solves the problem of Chinese garbled code

Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source

Ctfshow web getting started command execution web75-77

2021 q3-q4 investigation report on the use status of kotlin multiplatform
随机推荐
Redis cannot connect to the server through port 6379
Omnichannel membership - tmall membership 2: frequently asked questions
webgame开发中的文件解密
I want to know whether flush is a stock market? Is online account opening safe?
Question B of 2016 Sichuan Ti Cup Electronic Design Competition
Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
Build Pikachu shooting range and introduction
十大券商有哪些?手机开户安全么?
Omnichannel membership - tmall membership 1: opening tutorial
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
SQL injection in Pikachu shooting range
JS how to judge when data contains integer and floating-point types. Floating-point decimals retain two digits after the decimal point
Cross platform members get through the two channels of brand Ren Du
Research on the current situation of China's modified engineering plastics market and demand forecast analysis report 2022-2028
Generate JDE dot train
CG骨骼动画
2022 edition of China's cotton chemical fiber printing and dyeing Market Status Investigation and Prospect Forecast Analysis Report
TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?
Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files
2022 edition of China's energy and chemical industry market in-depth investigation and investment feasibility analysis report