当前位置: 首页 > 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;
}

相关文章:

  • 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.有效的括号
  • 福建福州马尾区区长王刚跨省份调任新疆生产建设兵团国资委主任
  • 越秀地产约41.49亿元出售北京海淀功德寺项目公司65%股权,此前已质押给华润置地
  • 第78届世界卫生大会20日审议通过“大流行协定”
  • 萨洛宁、康托罗夫、长野健……7月夏季音乐节来很多大牌
  • 澎湃读报丨多家央媒刊文关注拧紧纪律的螺丝:强化监督推动过紧日子要求落到实处
  • 中国预警机雷达有多强?可数百公里外看清足球轨迹