当前位置: 首页 > news >正文

算法之搜索--最长公共子序列LCS

最长公共子序列(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;
}

最长连续公共子序列


http://www.mrgr.cn/news/29805.html

相关文章:

  • 剃(磨)前插齿刀设计计算开发第二步:
  • Element Plus图片上传组件二次扩展
  • Android 中音频焦点的使用场景及示例
  • ssh远程连接try1账号切换tips
  • Java 之多线程高级
  • 计算机毕业设计 家电销售展示平台的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 使用python 将world的题库导入某学习软件的模板
  • 2024年9月python二级易错题和难题大全(附详细解析)(二)
  • MatchRFG:引领MemeCoin潮流,探索无限增长潜力
  • 论文不会写?分享6款AI论文写作免费一键生成网站!
  • Python urllib
  • LinuxC++的UDP服务器和客户端通信
  • 钢索缺陷检测系统源码分享
  • 1×1卷积核【super star 卷积核】
  • 人工智能与机器学习原理精解【21】
  • 图文检索(2):Visual-Linguistic Dependency Encoding for Image-Text Retrieval
  • 计算机网络的性能指标
  • 入门sentinel
  • 利用ClasserLoader来实现jar包加载并调用里面的方法
  • 英飞凌PSoC4000T示例工程