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

《算法笔记》11.8小节——动态规划专题->总结 问题 D: Coincidence

题目描述

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.

样例输入
google
goodbye
样例输出
4

分析:求两个字符串的最长公共子序列。注意是多组输入。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testchar s1[110],s2[110];while(~scanf("%s%s",s1,s2)){int dp[110][110];for(int i=0;i<=100;++i)for(int j=0;j<=100;++j)dp[i][j]=0;int l1=strlen(s1),l2=strlen(s2);for(int i=0;i<l1;++i){for(int j=0;j<l2;++j){if(s1[i]==s2[j])dp[i+1][j+1]=dp[i][j]+1;else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);}}printf("%d\n",dp[l1][l2]);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

http://www.dtcms.com/a/202269.html

相关文章:

  • linux下jenkins部署安装使用
  • 【图像大模型】Stable Video Diffusion:基于时空扩散模型的视频生成技术深度解析
  • 【HTML-2】HTML 标题标签:构建网页结构的基础
  • 设计模式之备忘录模式
  • 如何使用通义灵码玩转Python - AI编程助手提升效率
  • windows安装WS,实测可行
  • Prompt Tuning:轻量级大模型微调全攻略
  • Python | 库存覆盖天数【待续】
  • 理解阿里云的MQTT
  • Python类的力量:第六篇:设计模式——Python面向对象编程的“架构蓝图”
  • 面试突击:消息中间件之RabbitMQ
  • 《医院运营管理典型应用数据资源建设指南2025》全面分析
  • DeepSeek+PiscTrace+YOLO:迅速实现Mask掩码抠图
  • 【node.js】核心进阶
  • 在 Git 中添加子模块(submodule)的详细步骤
  • C++学习:六个月从基础到就业——多线程编程:互斥量与锁
  • Awesome ChatGPT Prompts:释放AI对话潜力的开源利器
  • Apache Apisix配置ip-restriction插件以限制IP地址访问
  • 【数据结构】
  • python-leetcode 68.有效的括号
  • 在Java项目中集成Deepseek大语言模型实践指南
  • AliSQL:阿里巴巴开源数据库的技术革新与应用实践
  • MySQL高可用之ProxySQL + MGR 实现读写分离实战
  • jmeter转义unicode变成中文
  • JMeter 教程:JSON 断言的简单介绍
  • 当PLC遇上电焊机器人:EtherCAT转CANopen上演工业级“语言翻译官”
  • Spring AI(7)——RAG
  • tigase源码学习笔记-事件总线EventBus
  • Pichome 开源网盘程序index.php 文件读取漏洞(CVE-2025-1743)
  • 【25软考网工】第七章 (2)UOS Linux文件和目录管理、用户和组管理