当前位置:网站首页>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
边栏推荐
- A solution of C batch query
- JS prototype and prototype chain
- 将OSS上的图片转换成Base64编码
- LabVIEW modify the appearance of the application window
- LabVIEW sets the transparency of the application display label
- Sqlserver edits data in the query interface (similar to Oracle's edit and ROWID)
- DW basic course (II)
- consul 开启健康监控检查
- Online Excel to CSV tool
- 从严从重从快 上海全面加强疫情期间食品安全监管
猜你喜欢
开发consul 客户端即微服务
Database experiment I database definition and data import
LabVIEW sets the transparency of the application display label
3、 Zygote start process
Introduction to hystrix and implementation of server fuse
Database Experiment four View experiment
MySQL back to table
Oracle ora-01033: Oracle initialization or shutdown in progressprocess solution
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
Ribbon组件负载均衡调用和使用
随机推荐
服务间通信方式
leetcode 2223 — 构造字符串的总得分和
LabVIEW采集鼠标、键盘数据
关于DateUtil时间工具类造成程序报错
三、zygote启动流程
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
consul server 服务注册中心安装
Ensuring the quality of living materials and food safety in Shanghai
[leetcode sword finger offer 28. Symmetric binary tree (simple)]
Error message: b04access.00f eve'. Read of address 000001B4
Database experiment VI integrity language experiment
RestTemplate 服务调用
构造函数 & 析构函数
Database Experiment 3 data update experiment
不同注册中心区别
Swift import third-party library reports an error no such module““
Hystrix断路器开启条件和流程以及默认备选处理
服务降级的实现
Pytorch installation (personal records)
MySQL back to table