当前位置:网站首页>【每日一练】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;
}
边栏推荐
- Typera+picgo+ Alibaba cloud OSS setup and error reporting solution [reprint]
- Productivity tool in the new era -- flowus information flow comprehensive evaluation
- systemverilog中function和task区别
- Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
- 线性代数(三)
- 编程大杂烩(二)
- 2021 ICPC Shaanxi warm up match b.code (bit operation)
- 弹性布局总结
- 计算BDP值和wnd值
- CSDN编程挑战赛之数组编程问题
猜你喜欢

CSDN编程挑战赛之数组编程问题
![Atof(), atoi(), atol() functions [detailed]](/img/5a/a421eab897061c61467c272f122202.jpg)
Atof(), atoi(), atol() functions [detailed]

Programming hodgepodge (I)

C100: smallest hevc visual IOT MCU

Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network

PHP warehouse inventory management system source code WMS source code

Airserver 7.3.0 Chinese version mobile device wireless transmission computer screen tool

New discovery of ROS callback function

HTB-Arctic

C编程 --“最大子数组的和” 的动态规划的解法
随机推荐
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
LCP plug-in creates peer-to-peer physical interface
C Programming -- the solution of dynamic programming of "the sum of the largest subarray"
Openfegin remote call lost request header problem
obj文件格式与.mtl文件格式
The difference between $write and $display in SystemVerilog
background
Please stop using system The currenttimemillis() statistical code is time-consuming, which is really too low!
QT qtextedit setting qscrollbar style sheet does not take effect solution
线性代数(三)
PMP Exam is easy to confuse concept discrimination skills! Don't lose points after reading!
Unity中使用UniRx入门总结
idea常用10个快捷键
flex布局常用属性总结
编程大杂烩(二)
New discovery of ROS callback function
Leetcode 204. 计数质数(太妙了)
Unity接入ChartAndGraph图表插件
Atof(), atoi(), atol() functions [detailed]
Introduction to interface in SystemVerilog