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

《算法笔记》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.xdnf.cn/news/7496.html

相关文章:

  • 业务流程和数据结构之间如何对应
  • Java集合框架详解:单列集合与双列集合
  • Wan2.1 图生视频 支持批量生成
  • 【QT】类A接收TCP数据并通过信号通知类B解析
  • mac .zshrc:1: command not found: 0 解决方案
  • 从头实现react native expo本地生成APK
  • 无线通信基础
  • 适合初学者的机器学习路线图
  • 【Linux】第二十四章 管理网络安全
  • windows/linux 模拟鼠标键盘输入
  • Python subprocess简单理解
  • layui 介绍
  • 如何保存解析后的商品信息?
  • Python合法图片爬虫开发全指南
  • 优化dp贪心数论
  • 深入解析Node.js文件系统(fs模块):从基础到进阶实践
  • React TS中如何化简DOM事件的定义
  • 【Linux】初见,基础指令
  • SMT贴片元器件识别要点与工艺解析
  • 经典面试题:TCP 三次握手、四次挥手详解
  • 基于ssm+mysql的在线CRM管理系统(含LW+PPT+源码+系统演示视频+安装说明)
  • 【Bluedroid】蓝牙HID Device virtual_cable_unplug全流程源码解析
  • Pycharm-jupyternotebook不渲染
  • 运行在华为云kubernetes应用接入APM服务
  • spark任务的提交流程
  • 不同净化技术(静电 / UV / 湿式)的性能对比研究
  • 刷题记录(5)链表相关操作
  • 门店管理五大痛点解析:如何用数字化系统实现高效运营
  • HomeAssistant开源的智能家居docker快速部署实践笔记(CentOS7)
  • 在tp6模版中加减法