• 动态规划


    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.背包问题

  • 相关阅读:
    leetcode 237: Delete Node in a Linked List
    谈谈地图中的道路绘制
    关于double类型数字相加位数发生变化的问题
    iOS_25_彩票设置的cell的数据源模型的封装
    session_start() [function.session-start]:
    Android系统Gps分析(一)【转】
    Android4.4 GPS框架分析【转】
    GPIO设备虚拟文件结点的创建【转】
    高通MSM8255 GPS 调试分析&&Android系统之Broadcom GPS 移植【转】
    和菜鸟一起学android4.0.3源码之硬件gps简单移植【转】
  • 原文地址:https://www.cnblogs.com/fripside/p/2928495.html
Copyright © 2020-2023  润新知