当前位置:网站首页>【每日一练】day(14)
【每日一练】day(14)
2022-07-25 05:42:00 【小唐学渣】
一、选择题
eg1


eg2

eg3

eg4

eg4

eg5

eg6

eg7

eg8

eg9

eg10

eg11

eg12

eg13

eg14

二、编程题
eg1

【解题思路】:
本题需要用动态规划求解,MCS[i][j]记录短字符串 s1 前 i 个字符和长字符串 s2 前 j 个字符的最长子串的长度,初始化所有值为 0。当 s1[i-1] = s2[j-1]时,MCS[i][j] = MCS[i - 1][j - 1] + 1,这里使用一个额外的值start 来记录最长子串在短字符串 s1 中出现的起始位置,maxlen记录当前最长子串的长度,当MCS[i][j] >maxlen 时,maxlen = MCS[i][j], 则start = i - maxlen ;档s1[i-1] != s2[j-1]时不需要任何操作,最后获取substr(start, maxlen)即为所求
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string GetSubStr(string& s1, string& s2)
{
if(s1.size() > s2.size())
swap(s1, s2);
int len1 = s1.size();
int len2 = s2.size();
//多开辟一行一列,初始化为0
vector<vector<int>> MSC(len1+1, vector<int>(len2+1, 0));
int max_size = 0;
int start = 0;
for(int i = 1; i < len1; ++i)
{
for(int j = 1; j < len2; ++j)
{
if(s2[j-1] == s1[i-1])
MSC[i][j] = MSC[i-1][j-1] + 1;
if(MSC[i][j] > max_size)
{
max_size = MSC[i][j];
start = i - max_size;
}
}
}
return s1.substr(start, max_size);
}
int main()
{
string s1;
string s2;
while(cin >>s1 >> s2)
{
string ret = GetSubStr(s1, s2);
cout << ret << endl;
}
return 0;
}
边栏推荐
- background
- Microservice - remote invocation (feign component)
- SystemVerilog中interface(接口)介绍
- 微服务 - 配置中心 - Nacos
- 微服务 - 网关Gateway组件
- y76.第四章 Prometheus大厂监控体系及实战 -- prometheus进阶(七)
- Microservice configuration center Nacos
- Switch NPM source to private source library
- R language obtains the data row where the nth maximum value of the specified data column is located in the data.table data
- HTB-Beep
猜你喜欢

LeetCode第302场周赛

Microservice gateway component

Programming hodgepodge (I)

Three schemes for finclip to realize wechat authorized login

Unity接入ChartAndGraph图表插件

Please stop using system The currenttimemillis() statistical code is time-consuming, which is really too low!

Unity accesses chartandgraph chart plug-in

Big talk · book sharing | Haas Internet of things device cloud integrated development framework

Productivity tool in the new era -- flowus information flow comprehensive evaluation

Application of hard coding and streaming integration scheme based on spice protocol in cloud games
随机推荐
The global shipment of glory 8x series exceeded 10million units, and the glory V20 exceeded 1.5 million units!
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
Programming hodgepodge (I)
微服务 - 配置中心 - Nacos
Switch NPM source to private source library
LCP plug-in creates peer VLAN interface
An SQL execution process
HTB-Devel
线性代数(三)
Sword finger offer 05. replace spaces
New discovery of ROS callback function
SystemVerilog中interface(接口)介绍
Singing "Seven Mile fragrance" askew -- pay tribute to Jay
QT qtextedit setting qscrollbar style sheet does not take effect solution
编程大杂烩(一)
Working principle and precautions of bubble water level gauge
MATLAB作图实例:5:双轴图
PostgreSQL learning 04 PG_ hint_ Plan installation and use, SQL optimization knowledge
编程大杂烩(二)
sqlilabs less-28~less-8a