-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathUVa12415.cc
285 lines (259 loc) · 7.59 KB
/
UVa12415.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// UVa12415 - Digit Patterns
// 陈锋
#include <cassert>
#include <functional>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
#include <unordered_map>
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
using namespace std;
typedef unsigned long long LL;
typedef std::vector<int> VI, *PVI;
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
bool first = true;
for(const auto& e : v){
if(first) first = false; else os <<" ";
os<<e;
}
return os;
}
struct ExpNode{
enum {A, STAR, OR, CONCAT};
int type, val;
ExpNode *l, *r;
ExpNode(int type, ExpNode *l = nullptr, ExpNode *r = nullptr, int val = -1)
: type(type), l(l), r(r), val(val) {};
~ExpNode() {
if(l) delete l;
if(r) delete r;
}
};
ostream& operator<<(ostream& os, ExpNode *pn) {
if(!pn) return os;
switch(pn->type) {
case ExpNode::A:
os<<(char)(pn->val);
break;
case ExpNode::STAR:
os<<"("<<pn->l<<")*";
break;
case ExpNode::OR:
os<<'('<<pn->l<<')'<<'+'<<'('<<pn->r<<')';
break;
case ExpNode::CONCAT:
os<<pn->l<<pn->r;
break;
default:
assert(false);
}
return os;
}
struct RexParser{
string rex;
int p, n;
void skip(char c) { p++; } // for debug purpose
ExpNode *item() { // (u)* || u closure
ExpNode *u;
if(rex[p] == '(')
skip(rex[p]), u = expr(), skip(')');
else
u = new ExpNode(ExpNode::A, nullptr, nullptr, rex[p++]);
while(rex[p] == '*')
skip(rex[p]), u = new ExpNode(ExpNode::STAR, u, nullptr);
return u;
}
ExpNode *concat() { // u1u2u3... concatenation
ExpNode *u = item();
while(p < n && rex[p] != ')' && rex[p] != '+')
u = new ExpNode(ExpNode::CONCAT, u, item());
return u;
}
ExpNode *expr() {
ExpNode *u = concat();
while(rex[p] == '+') {
skip(rex[p]);
u = new ExpNode(ExpNode::OR, u, concat());
}
return u;
}
ExpNode *parse(const string& str) {
rex = str, n = rex.length(), p = 0;
return expr();
}
};
template<int MAXS>
struct NFA{
struct Transition{
int ch, next;
Transition(int ch=0, int next=0):ch(ch),next(next){}
bool operator<(const Transition& rhs) const {
if(ch != rhs.ch) return ch < rhs.ch;
return next < rhs.next;
}
};
int n, MAXA; // num of states, alphabet size
typedef bitset<MAXS> SSet;
typedef std::vector<Transition> TVec;
TVec trans[MAXS];
vector<SSet> sNextCache[MAXS];
void add(int s, int t, int c=-1) { trans[s].push_back(Transition(c, t)); }
void process(ExpNode *u){
int st = n++, m;
switch(u->type) {
case ExpNode::A:
add(st, n, u->val);
break;
case ExpNode::STAR:
process(u->l);
add(st, st+1), add(st, n), add(n-1, st);
break;
case ExpNode::OR:
process(u->l);
m = n;
process(u->r);
add(st, st+1), add(st, m), add(m-1, n), add(n-1, n);
break;
case ExpNode::CONCAT:
add(st, st+1), process(u->l);
add(n-1, n), process(u->r);
add(n-1, n);
break;
default:
assert(false);
}
n++; // state 'end'
}
void init(const string& rex, int maxa) {
RexParser rp;
ExpNode *p = rp.parse(rex);
n = 0;
_for(i, 0, MAXS) trans[i].clear();
process(p);
MAXA = maxa;
delete p;
}
VI ss, es; // starting and ending states
void remove_epsilon() { // remove ε
VI reachable[MAXS], vis(MAXS, 0);
_for(i, 0, n){ // BFS to find epsilon-closure for each state
reachable[i].assign(1, i);
queue<int> q; q.push(i);
vis.assign(MAXS, 0), vis[i] = 1;
while(!q.empty()) {
int s = q.front(); q.pop();
for(const auto& ts : trans[s]){
if(ts.ch != -1) continue;
int s2 = ts.next;
if(vis[s2]) continue;
reachable[i].push_back(s2);
vis[s2] = 1;
q.push(s2);
}
}
}
ss = reachable[0];
_for(i, 0, n){ // merge transitions
set<Transition> tr;
for(auto& t : trans[i]) {
if(t.ch == -1) continue;
for(const auto r : reachable[t.next])
tr.insert(Transition(t.ch, r));
}
trans[i].assign(tr.begin(), tr.end());
}
buildNextCache();
}
void buildNextCache(){
_for(s, 0, n+1){
auto& sc = sNextCache[s];
sc.clear(), sc.resize(MAXA);
for(const auto& t : trans[s])
if(t.ch != -1) sc[t.ch].set(t.next);
}
}
void delta(int ch, const SSet& from, SSet& to) const { // δ
to.reset();
_for(s, 0, n+1) if(from.test(s)) to |= sNextCache[s][ch];
}
};
template<int MAXS>
ostream& operator<<(ostream& os, const NFA<MAXS>& nfa) {
os<<"starting: "<<nfa.ss<<", n = "<<nfa.n<<endl;
_for(s, 0, MAXS){
const typename NFA<MAXS>::TVec& ts = nfa.trans[s];
if(ts.empty()) continue;
os<<s<<" : ";
for(auto t : ts){
os<<", -"<<(t.ch == -1 ? 'e' : (char)(t.ch));
os<<"->"<<t.next;
}
os<<endl;
}
return os;
}
template<int MAXS>
struct HashDFA{
typedef NFA<MAXS> TNFA;
typedef typename TNFA::SSet SSet;
static const LL HX = 433494437;
unordered_map<LL, SSet> hashToS;
vector< unordered_map<LL, LL> > trans;
const TNFA *pnfa;
LL init(TNFA *nfa) {
hashToS.clear(), trans.clear();
assert(nfa); pnfa = nfa;
trans.resize(nfa->MAXA);
SSet s0;
for(auto s : nfa->ss) s0.set(s);
LL hash = calcHash(s0);
hashToS[hash] = s0;
return hash;
}
inline LL calcHash(const SSet& s){
LL ans = 0, X = 1;
_for(i, 0, MAXS) if(s[i]) ans += i * X, X *= HX;
return ans;
}
inline bool contains(LL hash, int s) {
assert(hashToS.count(hash));
return hashToS[hash].test(s);
}
inline LL delta(LL s, int ch) {
auto& m = trans[ch];
if(m.count(s)) return m[s];
const SSet& v = hashToS[s];
SSet next;
pnfa->delta(ch, v, next);
LL hash = calcHash(next);
hashToS[hash] = next;
return m[s] = hash;
}
};
const int MAXR = 500+4, MAXS = MAXR*4;
int main() {
int n; string rex, txt;
RexParser parser; NFA<MAXS> nfa; HashDFA<MAXS> hDfa;
while(cin>>n>>rex>>txt) {
nfa.init(rex, n+'1');
_for(i, 0, n) nfa.add(0, 0, i+'0');
nfa.remove_epsilon();
VI ans;
LL hs = hDfa.init(&nfa);
_for(i, 0, txt.size()){
if(i && hDfa.contains(hs, nfa.n-1)) ans.push_back(i);
hs = hDfa.delta(hs, txt[i]);
}
if(hDfa.contains(hs, nfa.n-1)) ans.push_back(txt.size());
cout<<ans<<endl;
}
return 0;
}
// 17336512 12415 Digit Patterns Accepted C++11 4.120 2016-05-09 13:53:08