题目
解题思路
使用正则表达式解决匹配问题:
- 将B串中的’?'转换为正则表达式[01]
- 使用HashSet去重存储匹配的子串
- 统计不同匹配的数量
关键点
- 正则表达式构建
- 子串匹配
- 使用Set去重
代码
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int solve(string& A, string& B) {
// 构建正则表达式模式
string pattern;
for (char c : B) {
if (c == '?') {
pattern += "[01]";
} else {
pattern += c;
}
}
// 存储所有可能的匹配结果
unordered_set<string> st;
// 遍历所有可能的子串
for (int i = 0; i <= A.length() - B.length(); i++) {
string sub = A.substr(i, B.length());
bool match = true;
// 手动匹配模式
for (int j = 0; j < B.length(); j++) {
if

最低0.47元/天 解锁文章
1439

被折叠的 条评论
为什么被折叠?



