zoukankan      html  css  js  c++  java
  • 算法问题实战策略 WILDCARD 递归 动态规划

    地址 https://algospot.com/judge/problem/read/WILDCARD

    解答 

    ? 比较好解决 比对两者字符串 字母一致则进到下一位 如果一个是?另一个无条件进到下一位

    * 则比较麻烦 需要遍历的进行检验

    如果w = *abc  p1 = abc p2 = fabc p3 = fzgxsfabc;

    所以我们需要略过 w[i+1] 分别于 p[j+0] p[j+1] p[j+2]......分别进行检验是否能够匹配

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    /*
    2
    he?p
    3
    help
    heap
    helpp
    *p*
    3
    help
    papa
    hello
    ===========================
    heap
    help
    help
    papa
    */
    
    int n, m;
    vector<string> matchStringV;
    string targetStr;
    
    bool match(const string& s, const string& w)
    {
        int pos = 0;
        while (pos < s.size() && pos < w.size() && (w[pos] == '?' || w[pos] == s[pos]))
            pos++;
        if (pos == w.size())
            return pos == s.size();
        if (w[pos] == '*') {
            for (int skip = 0; pos + skip <= s.size(); ++skip)
                if (match(s.substr(pos + skip),w.substr(pos + 1)))
                    return true;
        }
    
        return false;
    }
    
    void solve()
    {
        vector<string> ans;
        for (auto& e : matchStringV) {
            if (match(e, targetStr)) {
                ans.push_back(e);
            }
        }
    
        sort(ans.begin(), ans.end());
    
        for (auto& e : ans) {
            cout << e << endl;
        }
    
    }
    
    int main()
    {
        cin >> n;
        while (n--) {
            matchStringV.clear();
            cin >> targetStr;
            cin >> m;
            for (int i =0; i < m; i++) {
                string s;
                cin >> s;
                matchStringV.push_back(s);
            }
    
            solve();
        }
    }

     动态规划如下

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    /*
    2
    he?p
    3
    help
    heap
    helpp
    *p*
    3
    help
    papa
    hello
    ===========================
    heap
    help
    help
    papa
    */
    
    int n, m;
    vector<string> matchStringV;
    string targetStr;
    
    bool match(const string& s, const string& p)
    {
        int dp[200][200] = { 0 };
        dp[0][0] = 1;
    
        for (int i = 0; i < p.size(); i++)
        {
            if (p[i] == '*')
                dp[i + 1][0] = dp[i][0];
            for (int j = 0; j < s.size(); j++)
            {
                if (p[i] == '*') {
                    dp[i + 1][j + 1] |= dp[i + 1][j] || dp[i][j + 1];
                }
                else if (p[i] == '?') {
                    dp[i + 1][j + 1] |= dp[i][j];
                }
                else if (p[i] == s[j]) {
                    dp[i + 1][j + 1] |= dp[i][j];
                }
            }
        }
    
        return dp[p.size()][s.size()];
    }
    
    void solve()
    {
        vector<string> ans;
        for (auto& e : matchStringV) {
            if (match(e, targetStr)) {
                ans.push_back(e);
            }
        }
    
        sort(ans.begin(), ans.end());
    
        for (auto& e : ans) {
            cout << e << endl;
        }
    
    }
    
    int main()
    {
        cin >> n;
        while (n--) {
            matchStringV.clear();
            cin >> targetStr;
            cin >> m;
            for (int i = 0; i < m; i++) {
                string s;
                cin >> s;
                matchStringV.push_back(s);
            }
    
            solve();
        }
    }
    动态规划
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    可以说微软做的用户体验还不如这家泡菜厂
    如何在Visual Studio 工程之间共享静态内容 (js, css, img, etc.)
    使用JavaScript序列化任意复杂的对象
    使用ReSharper打造团队代码检查流程
    分享我们项目中基于EF事务机制的架构
    3句英文让你成为专业的欧美外包开发者
    linux 开机自启动脚本
    nginx 配置简单网站项目(linux下)
    python2 与 python3 如何实现共存
    centos 安装mysql
  • 原文地址:https://www.cnblogs.com/itdef/p/13062288.html
Copyright © 2011-2022 走看看