当前位置:网站首页>Luogu p3426 [poi2005]sza-template solution
Luogu p3426 [poi2005]sza-template solution
2022-06-26 13:21:00 【q779】
Luogu P3426 [POI2005]SZA-Template Answer key
Topic link :P3426 [POI2005]SZA-Template
The question : You are going to print a string of letters on the paper .
In order to finish the work , You decide to carve a seal . Every time the seal is used , Will put the seal on all Letters are printed on paper .
The same character in the same position can be printed many times . for example : use
abaThis seal can be printedababaThe job of ( In the middle of theaIt was printed twice ). however , Because what is printed cannot be erased , It is not allowed to print different characters in the same position . for example : useabaThis seal cannot be printedabcbaThe job of .Because it is not easy to carve seals , You want the string length of the seal to be as small as possible .
Enter a length no more than 5 × 1 0 5 5 \times 10^5 5×105 Non empty string for ( Contains only lowercase letters ), Represents the characters to be printed on the paper .
Because the seal can be printed at will , That is, there is no order or quantity
So we can consider what kind of situation can continue
Observe the examples
ababbababbabababbabababbababbaba
ababbaba
ababbaba
ababbaba
ababbaba
ababbaba
The front of ababbababbaba Very interesting
It can be found that the leftmost one must be printed once ( crap )
And the repeated printing is obviously the same as kmp Of border of
notes : The string s s s and 0 ≤ r < ∣ s ∣ 0 \le r < |s| 0≤r<∣s∣ ,
if s s s The length is r r r The prefix and length of are r r r The suffixes of are equal , It's called s s s The length is r r r The prefix for is s s s Of border.
Excerpt from oi-wiki
Consider first finding border, And then use dp Push
set up f i f_i fi Indicates printing s s s Of the i i i Minimum seal length of prefixes
The upper bound for f i = i f_i=i fi=i
here f i f_i fi Under certain conditions, it can be from fail i \text{fail}_i faili Transferred
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.
The first one looks complicated ,
In fact, that is ababbababbaba such border There is overlap
Just use a bucket for maintenance , Then just move it
Time complexity O ( n ) O(n) O(n)
Code :
#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;
}
Reprint please explain the source
边栏推荐
- Some conclusions about Nan
- F - Charm Bracelet
- Processing random generation line animation
- Sinotech software outsourcing
- Solutions to insufficient display permissions of find and Du -sh
- Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
- sed编辑器
- Don't mess with full_ Case and parallel_ CASE
- Reflect the technical depth (unable to speed up)
- 10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
猜你喜欢

Mode pont

防火墙介绍

Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program

Echart stack histogram: add white spacing effect setting between color blocks

C language: Exercise 2

Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里

IDC报告:百度智能云AI Cloud市场份额连续六次第一
![[how to connect the network] Chapter 2 (next): receiving a network packet](/img/f5/33e1fd8636fcc80430b3860d069866.png)
[how to connect the network] Chapter 2 (next): receiving a network packet

Word document export (using fixed template)
![Hdu1724[Simpson formula for integral]ellipse](/img/57/fb5098e150b5f3d91a5d0983a336ee.png)
Hdu1724[Simpson formula for integral]ellipse
随机推荐
5月产品升级观察站
MySQL数据库讲解(六)
Prototype
C - Common Subsequence
Basic configuration and test of Beifu twincat3 NCI in NC axis interface
Explain C language 10 in detail (C language series)
Echart stack histogram: add white spacing effect setting between color blocks
F - Charm Bracelet
Analysis of state transition diagram of Beifu NC axis
code force Party Lemonade
Reflect the technical depth (unable to speed up)
首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证
Electron official docs series: Get Started
Beifu PLC realizes data power-off maintenance based on cx5130
J - Wooden Sticks poj 1065
tauri vs electron
[how to connect the network] Chapter 2 (middle): sending a network packet
Mysql database explanation (6)
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
Mode pont