Codeforces Round #425 (Div. 2) - B
题目链接:http://codeforces.com/contest/832/problem/B
题意:给定一个好字母集合(只有小写字母,除了这些外其余都是坏字母集合),给定一个匹配模式串,
模式串只包含小写字母,'*','?'; 其中'?'可以替换成“好字母集合”中的任意一个字母
‘*’可以替换成空字符串,坏字母组成的字符串
然后给你n个字符串,问你这n个字符串哪些满足给定的匹配模式。
思路:按照题意模拟,或者直接写个正则表达式来匹配即可。
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<queue> #include<vector> #include<time.h> #include<cmath> using namespace std; typedef long long int LL; const int MAXN = 1e5 + 24; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; char gchar[26], pattern[MAXN], str[MAXN]; int vis[26]; int main(){ #ifdef kirito freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int n, slen, plen, glen; while (~scanf("%s", gchar)){ scanf("%s", pattern); scanf("%d", &n); plen = strlen(pattern); glen = strlen(gchar); bool flag = false; for (int i = 0; i < plen; i++){ if (pattern[i] == '*'){ flag = true; break; } } memset(vis, 0, sizeof(vis)); for (int i = 0; i < glen; i++){ vis[gchar[i] - 'a'] = 1; } for (int i = 1, j, k; i <= n; i++){ scanf("%s", str); slen = strlen(str); bool res = true; if (plen - 1 == slen&&flag){ int pos = 0; for (j = 0; j < slen&&res; j++, pos++){ if (pattern[pos] == '*'){ //换成空字符串 j--; } else if (pattern[pos] == '?'){ if (!vis[str[j] - 'a']){ res = false; } } else{ if (pattern[pos] != str[j]){ res = false; } } } } else if (plen <= slen){ int pos = 0, pslen = slen - plen; if (pslen&&!flag){ res = false; } for (j = 0; j < slen&&res; j++, pos++){ if (pattern[pos] == '*'){ //换成坏字母组成的字符串 for (k = j; k <= j + pslen; k++){ if (vis[str[k] - 'a']){ res = false; break; } } j = j + pslen; } else if (pattern[pos] == '?'){ if (!vis[str[j] - 'a']){ res = false; } } else{ if (pattern[pos] != str[j]){ res = false; } } } } else{ res = false; } printf(res ? "YES\n" : "NO\n"); } } return 0; }