zoukankan      html  css  js  c++  java
  • 动态规划

    1.求最长公共子串

    #pragma once
    
    #pragma once
    
    #include <random>
    #include <iostream>
    #include <tchar.h>
    #include <bitset>
    #include <cassert>
    #include <crtdbg.h>
    using namespace std;
    
    //-----------------------------------------------
    //a:97 z:122 A:65 Z:90
    //生成随机字符串
    //-----------------------------------------------
    void CreateStr(string& str,int len)
    {
        random_device rv;
        char* buffer = new char(len);
        assert(sizeof(buffer) > 0);
        for (int i = 0; i < len; ++i) {
            int ascii = 0;
            if (rv() % 2) {
                ascii = rv() % 25 + 97;
            } else { //大写
                ascii = rv() % 25 + 65; 
            }
            buffer[i] = ascii;
        }
        buffer[len] = '\0';
        str = buffer;
        cout<<"随机生成字符串:"<<endl;
        cout<<str<<endl;
    }
    
    void LCS(string stra,string strb,int lena,int lenb)
    {
        string str;
        int** array;
        array = new int*[lena];
        for (int i = 0; i < lena; ++i) {
            array[i] = new int[lenb];
        }
        array[0][0] = 0;
        for (int i = 0; i < lena; ++i) {
            array[i][0] = 0;
        }
    
        for (int j = 0; j < lenb; ++j) {
            array[0][j] = 0;
        }
    
        for (int i = 1; i < lena; ++i) {
            for (int j = 1; j < lenb; ++j) {
                if (stra[i] == strb[j]) {
                    //str += stra[i];
                    array[i][j] = array[i-1][j-1] + 1;
                    if (str.length() < array[i][j]) {
                        str += stra[i];
                    }
                } else {
                    array[i][j] = (array[i][j-1] > array[i-1][j]) ? array[i][j-1] : array[i-1][j];
                }
            }
        }
        cout<<str<<endl;
        cout<<array[lena-1][lenb-1]<<endl;
        for (int i = 0; i < lena; ++i) {
            delete [] array[i];
        }
        delete [] array;
    }
    
    
    int _tmain(int argc,TCHAR* argv[])
    {
        do {
            int in = 0;
            cin>>in;
            if (!in) {
                break;
            }
            string strA;
            string strB;
            CreateStr(strA,20);
            CreateStr(strB,14);
            LCS(strA,strB,20,14);
        } while (true);
        
        return 0;
    }

    2.管子切割问题

    3.矩阵乘法

    4.背包问题

  • 相关阅读:
    ubuntu 安装 systemback
    嵌入式gdb远程调试
    pecl 安装 phpredis
    Linux服务器内存池技术是如何实现的
    统一登录的几种常用设计模式
    数据库并发控制
    抽取一部分服务端做BFF(Backend For Frontend服务于前端的后端)
    干货 | 质量保障新手段,携程回归测试平台实践 原创 Sedro 携程技术 2021-01-21
    服务端绘图
    中间件技术在百度云原生测试中的应用实践 原创 路由器 百度智能化测试 今天
  • 原文地址:https://www.cnblogs.com/fripside/p/2928495.html
Copyright © 2011-2022 走看看