当前位置:网站首页>Leetcode 2223 -- sum of total scores of construction string
Leetcode 2223 -- sum of total scores of construction string
2022-04-23 22:06:00 【coding-hwz】
leetcode 2223 — Total score sum of construction string
One 、 Title Description

Two 、 Algorithm
This question is actually the subject of investigation Z Algorithm .
1、 Generalized problem description
For a length of n n n String s s s. Defined function z [ i ] z[i] z[i] Express s s s and s [ i : n − 1 ] s[i:n-1] s[i:n−1] The length of the longest common prefix . Give the calculation
z z z The algorithm of . notes : here s [ i : n − 1 ] s[i:n-1] s[i:n−1] contain s [ i ] s[i] s[i] and s [ n − 1 ] s[n-1] s[n−1].
2、 analysis
And KMP similar , Calculation z [ i ] z[i] z[i] We need to use the experience of the previous calculation process . The core of this algorithm is : Maintained [ l , r ] [l, r] [l,r] Section , Satisfy r = max 0 ≤ k < i k + z [ k ] − 1 (1) r = \max \limits_{0 \le k < i}k+z[k]-1\tag{1} r=0≤k<imaxk+z[k]−1(1) l + z [ l ] − 1 = r (2) l+z[l]-1=r\tag{2} l+z[l]−1=r(2)
It's not hard to find out , [ l , r ] [l, r] [l,r] It's actually s s s and s [ l : n − 1 ] s[l:n-1] s[l:n−1] The longest common prefix interval of . therefore , We can get s [ 0 : r − l ] = s [ l : r ] (3) s[0:r-l]=s[l:r]\tag{3} s[0:r−l]=s[l:r](3)
When we need to calculate z [ i ] z[i] z[i] when , It needs to be discussed on a case by case basis :
(1) If i ≤ r i\le r i≤r, Yes (3) Add... To the starting interval at the left and right ends of the equation at the same time i − l i-l i−l You can get s [ i − l : r − l ] = s [ i : r ] (4) s[i-l:r-l]=s[i:r]\tag{4} s[i−l:r−l]=s[i:r](4)
(a) if z [ i − l ] < r − i + 1 z[i-l] < r-i+1 z[i−l]<r−i+1, that s [ i − l : i − l + z [ i − l ] − 1 ] = s [ i : i + z [ i − l ] − 1 ] = s [ 0 : z [ i − l ] − 1 ] (5) s[i-l:i-l+z[i-l]-1]=s[i:i+z[i-l]-1]=s[0:z[i-l]-1] \tag{5} s[i−l:i−l+z[i−l]−1]=s[i:i+z[i−l]−1]=s[0:z[i−l]−1](5) s [ i − l + z [ i − l ] ] = s [ i + z [ i − l ] ] ≠ s [ z [ i − l ] ] (6) s[i-l+z[i-l]]=s[i+z[i-l]] \ne s[z[i-l]]\tag{6} s[i−l+z[i−l]]=s[i+z[i−l]]=s[z[i−l]](6)
therefore z [ i ] = z [ i − l ] (7) z[i] = z[i-l]\tag{7} z[i]=z[i−l](7)
(b) if z [ i − l ] ≥ r − i + 1 z[i-l] \ge r-i+1 z[i−l]≥r−i+1, Then we can expand (4) type s [ i − l : r − l ] = s [ i : r ] = s [ 0 : r − i ] (8) s[i-l:r-l]=s[i:r]=s[0:r-i] \tag{8} s[i−l:r−l]=s[i:r]=s[0:r−i](8)
At this time, we enumerate the following characters : z [ i ] = r − i + 1 + ( s [ r : n − 1 ] And s [ r − i + 1 : n − 1 ] Of most Long Male common front Affix District between Long degree ) (9) z[i]=r-i+1+(s[r:n-1] And s[r-i+1:n-1] The length of the longest common prefix interval )\tag{9} z[i]=r−i+1+(s[r:n−1] And s[r−i+1:n−1] Of most Long Male common front Affix District between Long degree )(9)
(2) If i > r i> r i>r, It indicates that you have not visited s [ i ] s[i] s[i], So let's go straight from s [ i ] s[i] s[i] Start solving violence .
We need to pay attention to , After each treatment , You need to judge whether to update [ l , r ] [l,r] [l,r] Section .
3、 Realization
vector<int> z_function(string s) {
int sLength = s.length(), left = 0, right = 0;
vector<int> z(sLength);
for (int i = 1; i < sLength; ++i) {
if (i <= right) {
if (z[i - left] < right - i + 1) {
z[i] = z[i - left];
}
else {
z[i] = right - i + 1;
while (i + z[i] < sLength && s[i + z[i]] == s[z[i]]) {
++z[i];
}
}
}
else {
z[i] = 0;
while (i + z[i] < sLength && s[i + z[i]] == s[z[i]]) {
++z[i];
}
}
if (i + z[i] - 1 > right) {
left = i;
right = i + z[i] - 1;
}
}
return z;
}
It's not hard to find out , Two else Branches can merge . therefore , The code can be reduced to :
vector<int> z_function(string s) {
int sLength= s.length(), left = 0, right = 0;
vector<int> z(sLength);
for (int i = 1; i < sLength; ++i) {
if (i <= right && z[i - left] < right - i + 1) {
z[i] = z[i - left];
}
else {
z[i] = max(0, right - i + 1);
while (i + z[i] < sLength && s[i + z[i]] == s[z[i]]) {
++z[i];
}
}
if (i + z[i] - 1 > right) {
left = i;
right = i + z[i] - 1;
}
}
return z;
}
The above code is derived from i = 1 i=1 i=1 Start calculating z [ i ] z[i] z[i] Because i = 0 i=0 i=0 Corresponding [ l , r ] [l,r] [l,r] Interval is [ 0 , n − 1 ] [0,n-1] [0,n−1], It is equivalent to solving by violence .
sumScores The implementation is as follows :
long long sumScores(string s) {
auto z = z_function(s);
return accumulate(z.begin(), z.end(), (long long)0) + s.length();
}

版权声明
本文为[coding-hwz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232157499594.html
边栏推荐
猜你喜欢

consul client客户端开发

Online timing flow chart making tool

OpenFeign超时设置

DW basic tutorial (I)

leetcode 2223 — 构造字符串的总得分和

1. Summary of GPIO control of nanopi M1 (Quanzhi H3) based on wiringpi

Database Experiment 7 stored procedure experiment

2. GPIO control summary (kernel driver) of nanopi M1 (Quanzhi H3)

在线Excel转CSV工具

Sqlserver edits data in the query interface (similar to Oracle's edit and ROWID)
随机推荐
Database Experiment 7 stored procedure experiment
Strictly, severely and quickly strengthen food safety supervision during the epidemic in Shanghai
基于Ribbon的服务调用
IIS cannot load * woff,*. woff2,*. Solution of SVG file
Hirschmann display maintenance computer controller repair
consul 开启健康监控检查
Online Excel to CSV tool
资本追逐Near生态
POI and easyexcel explanation
Pytorch deep learning practice (3)
Pytorch deep learning practice (2)
Mixed use of Oracle column row conversion and comma truncated string
Hystrix断路器开启条件和流程以及默认备选处理
Pycharm download and installation
LabVIEW collects mouse and keyboard data
C # problem of updating data: dynamic SQL generation is not supported for multiple base tables
Ribbon组件基本介绍和使用
LabVIEW显示控件中内容过长设置自动滚动条
hystrix dashboard的使用
Hystrix components