| 141 | |
| 142 | template<typename Ch> |
| 143 | static bool match(const std::basic_string<Ch>& text, const std::basic_string<Ch>& pattern) |
| 144 | { |
| 145 | if (pattern.empty()) return true; |
| 146 | if (pattern.size() == 1 && pattern.front() == static_cast<Ch>('*')) return true; |
| 147 | if (text.empty() && pattern.front() != static_cast<Ch>('*')) return false; |
| 148 | const int m = static_cast<int>(text.length()); |
| 149 | const int n = static_cast<int>(pattern.length()); |
| 150 | std::vector<std::vector<bool>> dp(m + 1, std::vector<bool>(n + 1, false)); |
| 151 | dp[0][0] = true; |
| 152 | for (int j = 1; j <= n; ++j) if (pattern[j - 1] == L'*') dp[0][j] = dp[0][j - 1]; |
| 153 | for (int i = 1; i <= m; ++i) |
| 154 | { |
| 155 | for (int j = 1; j <= n; ++j) |
| 156 | { |
| 157 | if (pattern[j - 1] == L'*') dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; |
| 158 | else if (pattern[j - 1] == L'?' || pattern[j - 1] == text[i - 1]) dp[i][j] = dp[i - 1][j - 1]; |
| 159 | } |
| 160 | } |
| 161 | return dp[m][n]; |
| 162 | } |
| 163 | static bool match(const std::wstring& text, const std::wstring& pattern) |
| 164 | { |
| 165 | return match<wchar_t>(text, pattern); |