zoukankan      html  css  js  c++  java
  • Daliy Algorithm (线性dp)-- day 68

    Nothing to fear


    种一棵树最好的时间是十年前,其次是现在!

    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    人一我十, 人十我百,追逐青春的梦想,怀着自信的心,永不言弃!
    2020.4.29


    编辑距离

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    const int N = 2005;
    char a[N] , b[N];
    int f[N][N];
    int main()
    {
    	scanf("%s %s",a + 1, b + 1);
    	int n = strlen(a + 1),m = strlen(b + 1);
    	for(int i = 0;i <= m ;i ++)f[0][i] = i;
    	for(int j = 0;j <= n; j ++)f[j][0] = j;
    
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= m ;j ++)
    		{
    			// 增加
    			f[i][j] = min(f[i][j-1] + 1,f[i-1][j] + 1);
    			// 改
    			f[i][j] = min(f[i][j],f[i-1][j-1] + (a[i] != b[j]));
    		}
    	}
    	cout << f[n][m] << endl;
    	return 0;
    }
    

    LCS

    常规版

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    const int N = 1005;
    int f[N][N];
    char a[N], b[N];
    int main()
    {
    	int n , m;
    	cin >> n >> m;
    	scanf("%s %s",a + 1, b + 1);
    
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= m ;j ++)
    		{
    			if(a[i] == b[j])
    			{
    				f[i][j] = max(f[i][j],f[i-1][j-1] + 1);
    			}else f[i][j] = max(f[i-1][j],f[i][j-1]); // 继承
    		}
    	}
    	cout << f[n][m] << endl;
    	return 0; 
    }
    
  • 相关阅读:
    通用js模块02:validutils.js
    通用js模块04:cookieUtils.js
    通用js模块03:formatutils.js
    通用js模块01:stringutils.js
    应用开发平台与代码生成工具感想
    linq to sql 中isnumeric的使用
    很惭愧啊
    错误:”未能加载文件或程序集“System.Web.Mvc, Version=2.0.0.0” 解决方法
    今天又温习了一下磁盘阵列的概念
    ashx的说明
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12805850.html
Copyright © 2011-2022 走看看