简答题
偏难
#7060
带通配符的字符串匹配 通配符是一类键盘字符,当我们不知道真正字符或者不想键入完整名字时,常常使用通配符代替一个或多个真正字符。通配符有问号(?)和星号(*)等,…
带通配符的字符串匹配
通配符是一类键盘字符,当我们不知道真正字符或者不想键入完整名字时,常常使用通配符代替一个或多个真正字符。通配符有问号(?)和星号(*)等,其中,“?”可以代替一个字符,而“*”可以代替零个或多个字符。
你的任务是,给出一个带有通配符的字符串和一个不带通配符的字符串,判断他们是否能够匹配。
例如,1?456 可以匹配 12456、13456、1a456,但是却不能够匹配23456、1aa456;
2*77?8可以匹配 24457798、237708、27798。
时间限制:1000
内存限制:65536
输入
输入有两行,每行为一个不超过20个字符的字符串,第一行带通配符,第二行不带通配符
输出
如果两者可以匹配,就输出“matched”,否则输出“not matched”
样例输入
1*456? 11111114567
样例输出
matched
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int f[3005][3005] = {0}, q[3005] = {0}, prt[3005][3005] = {0};
string st1, st2;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
void Get(int x, int y) {
if (x == 0 && y == 0)
return;
int lx = x - 1, ly = prt[x][y];
if (st1[x] == '*' && y != ly) {
q[++q[0]] = y - ly;
}
Get(lx, ly);
}
int main() {
cin >> st1 >> st2;
int i, j, k, bj, div, ans = 0, len1 = st1.length(), len2 = st2.length();
st1 = ' ' + st1;
st2 = ' ' + st2;
f[0][0] = 1;
for (i = 1; i <= len1; i++)
for (j = 0; j <= len2; j++) {
if (st1[i] == '?' || st1[i] == st2[j]) {
f[i][j] = f[i - 1][j - 1];
if (f[i][j])
prt[i][j] = j - 1;
} else if (st1[i] == '*') {
for (k = j, bj = 0; k >= 0; k--)
if (f[i - 1][k]) {
bj = 1;
break;
}
if (bj) {
f[i][j] = 1;
prt[i][j] = k;
}
}
}
if (f[len1][len2]) {
cout << "matched\n";
} else
cout << "not matched";
return 0;
}</pre>
这道题暂未补解析。