Files
exercises/dsa/ch4_string/dsa_0406_valid_string.cpp
T
2026-10-16 10:21:53 +08:00

59 lines
1.5 KiB
C++

#include <iostream>
#include <regex>
bool matches(std::string s, std::string pattern) {
int i = 0, j = 0;
bool within_bracket = false;
while (true) {
if (!within_bracket) {
if (pattern[j] == '[') {
within_bracket = true;
j++;
continue;
}
if (s[i] != pattern[j]) { return false; }
i++, j++;
} else {
bool matched = false;
while (pattern[j] != ']') {
matched = s[i] == pattern[j] || matched;
within_bracket = false;
j++;
}
if (!matched) { return false; }
i++, j++;
}
if (i == s.size() && j == pattern.size()) { return true; }
if (i == s.size() || j == pattern.size()) { return false; }
}
throw std::logic_error("What the fuck is this piece of WCJ doing?");
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::string pattern;
std::cin >> n;
std::vector<std::string> strs(n);
for (int i = 0; i < n; i++) {
std::cin >> strs[i];
}
std::cin >> pattern;
for (char& c : pattern) {
if (std::isupper(c)) { c -= 'A' - 'a'; }
}
for (int i = 0; i < n; i++) {
std::string s = strs[i];
for (char& c : s) {
if (std::isupper(c)) { c -= 'A' - 'a'; }
}
if (matches(s, pattern)) {
std::cout << i + 1 << ' ' << strs[i] << std::endl;
}
}
return 0;
}