当前位置:网站首页>ACM. HJ75 公共子串计算 ●●
ACM. HJ75 公共子串计算 ●●
2022-06-24 23:40:00 【chenyfan_】
HJ75 公共子串计算 ●●
描述
给定两个只包含小写字母的字符串,计算两个字符串的最大公共子串的长度。
注:子串的定义指一个字符串删掉其部分前缀和后缀(也可以不删)后形成的字符串。
数据范围:字符串长度: 1 ≤ s ≤ 150 1\le s\le 150 1≤s≤150
进阶:时间复杂度: O ( n 3 ) O(n^3) O(n3),空间复杂度: O ( n ) O(n) O(n)
输入描述:
输入两个只包含小写字母的字符串
输出描述:
输出一个整数,代表最大公共子串的长度
示例
输入:
asdfas
werasdfaswer
输出:
6
题解
本题与 LC 718. 最长重复子数组类似。
解法可见 C++ 数据结构与算法(十三)(动态规划 – 打家劫舍、股票问题、子序列问题)。
1. 动态规划
dp[i][j]表示以s1[i-1]、s2[j-1]为结尾(一定包括这两个字符)的字符串的公共子串长度(不一定是最大);- 初始化为0;
- 如果
s1[i-1] == s2[j-1],那么dp[i][j] = dp[i-1][j-1] + 1;,并比较更新最大长度;
否则,字符串结尾不相等,即公共长度为0. - 从前往后遍历。

- 时间复杂度: O ( n × m ) O(n × m) O(n×m),n 为A长度,m为B长度
- 空间复杂度: O ( n × m ) O(n × m) O(n×m)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s1, s2;
while(cin >> s1 >> s2){
int n = s1.length(), m = s2.length(), ans = 0;
vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
if(s1[i-1] == s2[j-1]){
// 所选字符串结尾相等
dp[i][j] = dp[i-1][j-1] + 1; // 上一串长度 + 1
ans = max(ans, dp[i][j]);
} // s1[i-1] != s2[j-1] 即结尾不相同,长度为0
}
}
cout << ans << endl;
}
return 0;
}
- 一维滚动数组优化空间复杂度 O(n)
dp[i][j] 都是由dp[i - 1][j - 1]推出。那么压缩为一维数组,也就是dp[j]都是由dp[j - 1]推出。
也就是相当于可以把上一层dp[i - 1][j]拷贝到下一层dp[i][j]来继续用。
此时遍历B数组的时候,就要从后向前遍历,这样避免重复覆盖,且长度为 0 时要注意赋值。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s1, s2;
while(cin >> s1 >> s2){
int n = s1.length(), m = s2.length(), ans = 0;
vector<int> dp(m+1, 0);
for(int i = 1; i <= n; ++i){
for(int j = m; j >= 1; --j){
if(s1[i-1] == s2[j-1]){
dp[j] = dp[j-1] + 1; // 相等
ans = max(ans, dp[j]);
}else{
dp[j] = 0; // 不相等
}
}
}
cout << ans << endl;
}
return 0;
}
2. 滑动窗口
通过滑动字符串来遍历所有字符串重叠的情况,然后在重叠部分找最大公共子串的长度。
如图可知,枚举 字符串 所有的重叠(对齐)方式主要分为三步(s1长度更小):
(1)s1 尾部开始与 s2 重叠,直到 s1 完全被覆盖;
(2)s1 头部与 s2 头部开始错位,直到 s1 尾部 与 s2 尾部重叠;
(3)s1 尾部超过 s2 尾部,直到不再重叠。
每次滑动操作需要更新两个字符串的重叠起始下标和结束下标。
- 时间复杂度: O ( ( N + M ) × min ( N , M ) ) O((N + M) \times \min(N, M)) O((N+M)×min(N,M))
- 空间复杂度: O ( 1 ) O(1) O(1)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int getLen(string s1, string s2, int a1, int b1, int a2, int b2){
// 找到s1[a1, b1]和s2[a2,b2]的最大公共子串长度
int n = b1 - a1 + 1;
int pre = 0, ans = 0;
for(int i = 0; i < n; ++i){
if(s1[a1 + i] == s2[a2 + i]){
ans = max(ans, pre + 1);
++pre;
}else{
pre = 0;
}
}
return ans;
}
int main(){
string s1, s2;
// 滑动窗口
while(cin >> s1 >> s2){
if(s1.length() > s2.length()) swap(s1, s2);
int n = s1.length(), m = s2.length(), ans = 0; // s1 更短
for(int i = 1; i <= n; ++i){
// s1 尾部开始与 s2 重叠,直到 s1 完全被覆盖
int subLen = getLen(s1, s2, n-i, n-1, 0, i-1);
ans = max(ans, subLen);
}
for(int i = 1; i <= m-n; ++i){
// s1 头部与 s2 头部开始错位,直到 s1 尾部 与 s2 尾部重叠
int subLen = getLen(s1, s2, 0, n-1, i, n+i-1);
ans = max(ans, subLen);
}
for(int i = 1; i < n; ++i){
// s1 尾部超过 s2 尾部,直到不再重叠
int subLen = getLen(s1, s2, 0, n-1-i, m-2-n+i, m-1);
ans = max(ans, subLen);
}
cout << ans << endl;
}
return 0;
}
边栏推荐
- 中信证券手机开户是靠谱的吗?安全吗
- F - Spices(线性基)
- ACL access control of squid proxy server
- 云原生数据库VS传统数据库
- 调用系统函数安全方案
- Redis
- File system - basic knowledge of disk and detailed introduction to FAT32 file system
- Application of TSDB in civil aircraft industry
- I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
- The role of software security testing, how to find a software security testing company to issue a report?
猜你喜欢

自动化测试

3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?

华为、阿里等大厂程序员真的好找对象吗?

qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”

折叠屏将成国产手机分食苹果市场的重要武器

Software testing salary in first tier cities - are you dragging your feet

Can automate - 10k, can automate - 20K, do you understand automated testing?
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/ad/26a302ca724177e37fe123f8b75e4e.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

李宏毅《机器学习》丨6. Convolutional Neural Network(卷积神经网络)

都2022年了,你还不了解什么是性能测试?
随机推荐
折叠屏将成国产手机分食苹果市场的重要武器
Smartctl opens the device and encounters permission denied problem troubleshooting process record
F - spices (linear basis)
E - average and median
Unity存档系统——Json格式的文件
电脑端微信用户图片DAT格式解码为图片(TK版)
数据库系统概论必背知识
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
Leecode learning notes - the shortest path for a robot to reach its destination
[day 26] given the ascending array nums of n elements, find a function to find the subscript of target in nums | learn binary search
Call system function security scheme
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
E - Average and Median(二分)
中信证券手机开户是靠谱的吗?安全吗
Post competition summary of kaggle patent matching competition
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
PyTorch学习笔记(七)------------------ Vision Transformer
Modifying universal render data at runtime
PE文件基础结构梳理
Qt中使用QDomDocument操作XML文件