http://codeforces.com/contest/852/problem/G
题意:给你 n 个字符串和 m 次查询,每次给你一个匹配串,输出匹配串能与 n 个字符串匹配的个数。
匹配规则,’?’ 可以与任何单字符匹配,‘?’ 也可以和空字符匹配,其他字符只能与自身相同字符匹配。
题解:字典树模板题。
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; const int MAXN = 100000+10; int ans; struct node { node* next[5]; int index; int f; node() { for(int i = 0; i < 5; i++) { next[i] = NULL; } index = 0; f = 0; } }*root; void Insert(string s) { node* rt = root; int len = s.size(); for(int i = 0; i < len; i++) { int j = s[i]-'a'; if(rt->next[j] == NULL) { rt->next[j] = new node(); } rt = rt->next[j]; } rt->index++; return ; } void solve(node* rot, string s, int id, int m) { node* rt = rot; int len = s.size(); while( s[id] != '?' && id < len ) { int j = s[id]-'a'; if( rt->next[j] != NULL ) { rt = rt->next[j]; id++; } else return ; } if( id == len ) { if(rt->f < m) { ans += rt->index; rt->f = m; } return ; } for(int i = 0; i < 5; i++) { if( rt->next[i] != NULL ) { solve(rt->next[i], s, id+1, m); } } solve(rt, s, id+1, m); return ; } int main (void) { ios::sync_with_stdio(false); root = new node(); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++ ) { string str; cin >> str; Insert( str ); } for(int i = 1; i <= m; i++) { ans = 0; string str; cin >> str; solve(root, str, 0, i); cout << ans << endl; } }