zoukankan      html  css  js  c++  java
  • 最长公共子序列、最长递增子序列、最长递增公共子序列、最长子序列(leetcode 524)

    参考:https://www.cnblogs.com/sasuke-/p/5396843.html

     https://blog.csdn.net/someone_and_anyone/article/details/81044153

    https://blog.csdn.net/someone_and_anyone/article/details/81044153

     https://blog.csdn.net/someone_and_anyone/article/details/81044153

    最长子序列(leetcode 524)

    用每个字符串同当前字符串进行匹配,看当前字符串是否是要匹配字符串的子串。

    对所有字符串先排字典序,然后依次同要匹配的字符串匹配。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    class Solution {
    public:
        string findLongestWord(string s, vector<string>& d) {
            sort(d.begin(), d.end());
            int s_len = s.length();
            string tmp = "";
            int len_now = -1;
            for(auto it=d.begin(); it!=d.end(); it++)
            {
                int i_len = (*it).length();
    
                if(s_len < i_len && i_len < len_now)
                {
                    continue;
                }
                if(isValid(s, (*it)) && i_len > len_now)
                {
                    tmp = (*it);
                    len_now = i_len;
                }
            }
            return tmp;
        }
    
        bool isValid(string& s1, string& s2)
        {
            int len1 = s1.length(), len2 = s2.length();
            int i=0,j=0;
            for(; i<len1 && j < len2;)
            {
                if(s1.at(i) == s2.at(j))
                {
                    j++;
                }
                i++;
            }
    
            return j == len2;
        }
    };
    
    int main()
    {
        Solution sol;
        vector<string> s_v = {"ale","apple","monkey","plea"};
        string s1 = "abpcplea";
    
        string res = sol.findLongestWord(s1, s_v);
        cout << res << endl;
        return 0;
    }
    View Code
  • 相关阅读:
    js单体模式
    react实现递归搜索下拉查询目录树功能
    浏览器跨域问题分析
    css中清除浮动
    ts中的函数
    ts中类型
    RX.js6变化
    js对象模型3
    React数组变化之后,视图没有更新
    Mac安装yarn并配置环境变量PATH,运行报错问题解决
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/10525589.html
Copyright © 2011-2022 走看看