-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcj2009R0.cc
195 lines (177 loc) · 6.1 KB
/
gcj2009R0.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Time-stamp: <2014-08-17 22:27:38 scinart>
// created at 2014-08-17 21:13:13
// gcj2009R0.cc
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <cmath>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <sstream>
using namespace std;
#define REP(i, n) for (int i=0; i<(int)(n); ++i)
#define REP_1(i, n) for (int i=1; i<=(int)(n); ++i)
#define FOR(i, b, e) for (int i=(b); i<(int)(e); ++i)
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
#define DWN(i, b, e) for (int i=(b); i>(int)(e); --i)
#define ALL(c) c.begin(), c.end()
#ifdef __GNUC__
#if __cplusplus >= 201103L
#define EACH(it,A) for (auto it=A.begin(); it!=A.end(); ++it)
#define REACH(it,A) for (auto it=A.rbegin(); it!=A.rend(); ++it)
#else
#ifdef __STRICT_ANSI__
#define EACH(it,A) for (__typeof__(A.begin()) it=A.begin(); it!=A.end(); ++it)
#define REACH(it,A) for (__typeof__(A.rbegin()) it=A.rbegin(); it!=A.rend(); ++it)
#else
#define EACH(it,A) for (typeof(A.begin()) it=A.begin(); it!=A.end(); ++it)
#define REACH(it,A) for (typeof(A.rbegin()) it=A.rbegin(); it!=A.rend(); ++it)
#endif
#endif
#else
#define EACH(it,A) for (auto it=A.begin(); it!=A.end(); ++it)
#define REACH(it,A) for (auto it=A.rbegin(); it!=A.rend(); ++it)
#endif
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> PII;
typedef map<int,int> MII;
typedef map<string,int> MSI;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
bool PIIfs(const PII& a, const PII& b){return a.first<b.first || (a.first == b.first && a.second<b.second);}
bool PIIsf(const PII& a, const PII& b){return a.second<b.second || (a.second == b.second && a.first<b.first);}
bool PIIFS(const PII& a, const PII& b){return a.first>b.first || (a.first == b.first && a.second>b.second);}
bool PIISF(const PII& a, const PII& b){return a.second>b.second || (a.second == b.second && a.first>b.first);}
bool PIIfS(const PII& a, const PII& b){return a.first<b.first || (a.first == b.first && a.second>b.second);}
bool PIIsF(const PII& a, const PII& b){return a.second<b.second || (a.second == b.second && a.first>b.first);}
bool PIIFs(const PII& a, const PII& b){return a.first>b.first || (a.first == b.first && a.second<b.second);}
bool PIISf(const PII& a, const PII& b){return a.second>b.second || (a.second == b.second && a.first<b.first);}
template <typename T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <typename T> inline T max(T a, T b, T c, T d) { return max(max(a, b, c), d); }
template <typename T> inline T max(T a, T b, T c, T d, T e) { return max(max(a, b, c, d), e); }
template <typename T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <typename T> inline T min(T a, T b, T c, T d) { return min(min(a, b, c), d); }
template <typename T> inline T min(T a, T b, T c, T d, T e) { return min(min(a, b, c, d), e); }
template <typename T> inline istream& RD(T& x){return cin>>x;}
template <typename T, typename U> inline istream& RD(T& a, U& b){return cin>>a>>b;}
template <typename T, typename U, typename V> inline istream& RD(T& a, U& b, V& c){return cin>>a>>b>>c;}
template <typename T, typename U, typename V, typename W>
inline istream& RD(T& a, U& b, V& c,W& d){return cin>>a>>b>>c>>d;}
template <typename T, typename U, typename V, typename W, typename X>
inline istream& RD(T& a, U& b, V& c,W& d,X& e){return cin>>a>>b>>c>>d>>e;}
template <typename T>
void checkmin(T& a,const T& b){if(b<a)a=b;}
template <typename T>
void checkmax(T& a, const T& b){if(b>a)a=b;}
const int MAXN = 100000;
const int Sigma = 26; //字母表大小
const char Alpha = 'a';//字母表首字母
struct Node
{
bool point;
Node* child[Sigma];
void* operator new(size_t, void *p){ return p; }
} buffer[MAXN];
int LEN,DICSIZE,Q;
class SimpleTrie
{
public:
Node* root; //¡important! root->fail MUST be NULL.
Node* data;
public:
SimpleTrie():data(buffer){memset(buffer,0,sizeof(buffer));root = new((void*)data++) Node;}
public:
void insert(const string& s) {
Node* p = root;
for (int i=0; i<LEN; i++) {
int where=s[i]-'a';
if (p->child[where]==NULL) p->child[where]=new((void*)data++) Node;
p=p->child[where];
}
p->point=true;
}
};
int main()
{
#ifdef ECLIPSE
freopen("in.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin>>LEN>>DICSIZE>>Q;
SimpleTrie trie;
REP(i,DICSIZE)
{
string s;
cin>>s;
trie.insert(s);
}
REP_1(qq,Q)
{
string q;
cin>>q;
std::vector<Node*> mainq;
std::vector<Node*> aidq;
mainq.push_back(trie.root);
int inpar=false;
EACH(it,q)
{
switch(*it)
{
case ')':
{
inpar=false;
mainq.swap(aidq);
aidq.clear();
continue;
}
case '(':
{
inpar=true;
continue;
}
default:
{
unsigned char c=*it-Alpha;
if(inpar)
{
EACH(jt,mainq)
if((*jt)->child[c]!=NULL)
aidq.push_back((*jt)->child[c]);
}
else
{
EACH(jt,mainq)
if((*jt)->child[c]!=NULL)
aidq.push_back((*jt)->child[c]);
mainq.swap(aidq);
aidq.clear();
}
}
}
}
cout<<"Case #"<<qq<<": "<<mainq.size()<<'\n';
}
return 0;
}