算法问题实战策略 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驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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匹配 轻量级