Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active June 3, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/f10f78fdc1dc148fb5f658f2eb5ecff5 to your computer and use it in GitHub Desktop.
Save fpdjsns/f10f78fdc1dc148fb5f658f2eb5ecff5 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <iostream>
#define SIZE 101
using namespace std;
vector<vector<int>> dp;
string pattern;
string s;
int pSize = pattern.size();
int sSize = s.size();
/*
* 시간복잡도 : O(|pattern||s|)
*/
int solve(int pInd, int sInd) {
int& ret = dp[pInd][sInd];
if (ret != -1) return ret;
ret = false;
if (pSize == pInd && sSize == sInd) return true;
if (pSize < pInd || sSize < sInd) return false;
if (pattern[pInd] == '*') {
for (int i = 0; i <= sSize - sInd; i++) {
ret |= solve(pInd + 1, sInd + i);
}
}
else if (pattern[pInd] == '?') {
return ret = solve(pInd + 1, sInd + 1);
}
else {
if (pattern[pInd] != s[sInd])
return ret = false;
return ret = solve(pInd + 1, sInd + 1);
}
return ret;
}
int main() {
int C;
cin >> C;
while (C--) {
cin >> pattern;
pSize = pattern.size();
int N;
cin >> N;
set<string> answerSet;
while (N--) {
cin >> s;
dp = vector<vector<int>>(SIZE, vector<int>(SIZE, -1));
sSize = s.size();
if (solve(0, 0) > 0) {
answerSet.insert(s);
}
}
for (set<string>::iterator it = answerSet.begin(); it != answerSet.end(); it++) {
cout << *it << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment