当前位置: 首页> 健康> 科研 > 免费咨询在线聊天_帝国cms建站教程_网络推广服务合同范本_免费建站平台

免费咨询在线聊天_帝国cms建站教程_网络推广服务合同范本_免费建站平台

时间:2025/7/25 21:58:51来源:https://blog.csdn.net/sinat_40546227/article/details/114795742 浏览次数:0次
免费咨询在线聊天_帝国cms建站教程_网络推广服务合同范本_免费建站平台

最长公共子序列(longest common sequence):可以不连续
在这里插入图片描述

最长公共子串(longest common substring):连续
在这里插入图片描述

demo

for (int i = 1;i<=lena;i++){for (int j = 1;j<=lenb;j++){if(a[i-1]==b[j-1]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}
}

Coincidence

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb
Find a longest common subsequence of two strings.

输入描述:

First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.

输出描述:

For each case, output k – the length of a longest common subsequence in one line.

代码
#include <bits/stdc++.h>
using namespace std;int dp[105][105];void LCS(string a,string b){int lena = a.length();int lenb = b.length();for(int i = 1;i<=lena;i++){for(int j = 1;j<=lenb;j++){if(a[i-1]==b[j-1]){//注意不是i,jdp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}}
}int main(){string a,b;while(cin>>a>>b){int lena = a.length();int lenb = b.length();LCS(a,b);cout<<dp[lena][lenb]<<endl;}return 0;
}

最长公共子序列

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb
给出两个字符串序列,求最长公共子序列(LCS) 。(改编版,原题规定两字符串长度相等,且无重复元素。)

输入描述:

多组数据输入。
在一行分别输入两个字符串。(字符串长度<=1000)

输出描述:

输出两个字符串的最长公共子序列的长度。

代码
#include <bits/stdc++.h>
using namespace std;int dp[1005][1005];void LCS(string a,string b){int lena = a.length();int lenb = b.length();for(int i = 1;i<=lena;i++){for(int j = 1;j<=lenb;j++){if(a[i-1]==b[j-1]){//注意不是i,jdp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}}
}int main(){string a,b;while(cin>>a>>b){int lena = a.length();int lenb = b.length();LCS(a,b);cout<<dp[lena][lenb]<<endl;}return 0;
}

最长连续公共子序列

关键字:免费咨询在线聊天_帝国cms建站教程_网络推广服务合同范本_免费建站平台

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: