算法问题实战策略 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();
    }
}
动态规划
复制代码

 

posted on   itdef  阅读(419)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2019-06-07 acwing 110 防晒
2015-06-07 c++11多线程学习笔记之二 mutex使用
2015-06-07 c++11多线程学习笔记之一 thread基础使用
2014-06-07 redis学习笔记
2014-06-07 ip白名单 通过* ? 检测IP匹配 轻量级

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示