-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathans.cpp
372 lines (349 loc) · 6.31 KB
/
ans.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include<bits/stdc++.h>
using namespace std;
int titie = 0;
struct node
{
bool fin;
map<char, vector<node*>> trans;//epsilon is '0'
int id;
node()
{
fin = false;
id = titie;
titie++;
}
};
struct nfa
{
node *start, *end;
nfa(char c)
{
//anything between '' is a node
//---char---> indicates that this is a transition to be taken from a node if char is encountered
//'start'---c--->'end'
start = new node();
end = new node();
start->trans[c].push_back(end);
end->fin = true;
}
};
stack<char> chars;
stack<nfa*> nfas;
//add functions for +, |, * and .
void uni()
{
nfa* t1 = nfas.top();
nfas.pop();
nfa* t2 = nfas.top();
nfas.pop();
t1->end->fin = false;
t1->end->trans['0'].push_back(t2->end);
t1->start->trans['0'].push_back(t2->start);
t2->start = t1->start;
nfas.push(t2);
return;
}
void prod()
{
nfa* t1 = nfas.top();
nfas.pop();
nfa* t2 = nfas.top();
nfas.pop();
t2->end->fin = false;
t2->end->trans['0'].push_back(t1->start);
t2->end = t1->end;
nfas.push(t2);
return;
}
void one_or_more()
{
nfa* t2 = nfas.top();
nfas.pop();
t2->end->trans['0'].push_back(t2->start);
nfas.push(t2);
return;
}
void z_or_more()
{
nfa* t2 = nfas.top();
nfas.pop();
node* temp = new node();
temp->fin = true;
t2->end->fin = false;
t2->end->trans['0'].push_back(t2->start);
t2->start->trans['0'].push_back(temp);
t2->end = temp;
nfas.push(t2);
return;
}
void reg_to_nfa(string& reg)
{
//concat is '.'
//zero or more is '*'
//union is '|'
//one or more is '+'
nfa* basic;
for(int i = 0;i<reg.length();i++)
{
if(reg[i] == '(' || reg[i] == '.' || reg[i] == '|')
{
chars.push(reg[i]);
continue;
}
if(reg[i] == ')')
{
chars.pop();
if(!chars.empty())
{
if(chars.top() == '|')
{
chars.pop();
uni();
}
else if(chars.top() == '.')
{
chars.pop();
prod();
}
}
continue;
}
if(reg[i] == '+')
{
one_or_more();
continue;
}
if(reg[i] == '*')
{
z_or_more();
continue;
}
//we reach here only if the current character is 'a' or 'b'
basic = new nfa(reg[i]);
nfas.push(basic);
}
return;
}
void check_nfa_validity()
{
nfa* on_top = nfas.top();
node* tempo = on_top->start;
int c = 0;//the number of nodes in the nfa
map<node*, int> vis;
queue<node*> q;
q.push(tempo);
vis[tempo] = 1;
while(!q.empty())
{
c++;
node* t = q.front();
q.pop();
cout<<1<<" "<<t->id<<" "<<t->fin<<endl;
for(auto it:t->trans)
{
cout<<2<<" "<<it.first<<endl;
for(auto bot:it.second)
{
cout<<3<<" "<<bot->id<<endl;
if(vis[bot] == 0)
{
vis[bot] = 1;
q.push(bot);
}
}
}
cout<<endl;
}
cout<<c<<endl;
}
//dfs function to check whether the entire string passed can be accepted by this regex or not
bool mine(int ind, int n, node* nigga, string& s)
{
if(ind > n && nigga->fin)
{
return true;
}
bool retval = false;
if(nigga->trans.find('0') != nigga->trans.end())
{
for(auto nigger:nigga->trans['0'])
{
retval = (retval | mine(ind, n, nigger, s));
if(retval)
{
return retval;
}
}
}
if(ind <= n && nigga->trans.find(s[ind]) != nigga->trans.end())
{
for(auto nigger:nigga->trans[s[ind]])
{
retval = (retval | mine(ind + 1, n, nigger, s));
if(retval)
{
return retval;
}
}
}
return retval;
}
string parser(string& s)
{
// int n = s.length() - 1;
// node* starter = nfas.top()->start;
// nfas.pop();
// string ans = "";
// int i = 0;
// while(i <= n)
// {
// int big = 0, bigj = i, j = i;
// while(j <= n)
// {
// string temp = s.substr(i, j - i + 1);
// bool rec = mine(0, j - i, starter, temp);
// if(rec)
// {
// if(j - i + 1 > big)
// {
// big = j - i + 1;
// bigj = j;
// }
// }
// j++;
// }
// if(big == 0)
// {
// ans.push_back('@');
// ans.push_back(s[i]);
// }
// else
// {
// ans.push_back('$');
// ans.append(s.substr(i, bigj - i + 1));
// }
// i = bigj + 1;
// }
// ans.push_back('#');
// return ans;
//optimised version
int n = s.length() - 1;
node* starter = nfas.top()->start;
string ans = "";
int i = 0;
while(i <= n)
{
int big = 0, j = 0;//j is actually length - 1
while(i + j <= n)
{
bool rec = mine(i, i + j, starter, s);
if(rec)
{
if(j + 1 > big)
{
big = j + 1;
}
}
j++;
}
//cout<<big<<" "<<i<<" "<<j<<endl;
if(big == 0)
{
ans.push_back('@');
ans.push_back(s[i]);
i++;
}
else
{
ans.push_back('$');
ans.append(s.substr(i, big));
i = i + big;
}
}
ans.push_back('#');
return ans;
}
bool will_accept_empty_or_not(node* nigga)
{
if(nigga->fin)
{
return true;
}
bool retval = false;
if(nigga->trans.find('0') != nigga->trans.end())
{
for(auto nigger:nigga->trans['0'])
{
retval = (retval | will_accept_empty_or_not(nigger));
if(retval)
{
return retval;
}
}
}
return retval;
}
string input_string()
{
string s;
cin>>s;
return s;
}
void print(string& s)
{
cout<<s<<endl;
return;
}
string insert_product_sign(string& reg)
{
string temp = "";
for(int i = 0;i<reg.length();i++)
{
if(temp.length() && temp[temp.length() - 1] == ')' && reg[i] == '(')
temp.push_back('.');
temp.push_back(reg[i]);
}
return temp;
}
bool in_to_fin_for_free(string& s)
{
bool rec = will_accept_empty_or_not(nfas.top()->start);
bool retval = false;
if(s.length() == 0)
{
if(rec)
{
cout<<"$#"<<endl;
retval = true;
}
else
{
cout<<"@#"<<endl;
retval = true;
}
}
return retval;
}
int main()
{
#ifndef INPUT
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string reg = input_string();
string s = input_string();
reg = insert_product_sign(reg);
//cout<<reg<<endl;
reg_to_nfa(reg);
if(in_to_fin_for_free(s))
return 0;
//check_nfa_validity();
//int n = s.length() - 1;
//node* starter = nfas.top()->start;
//cout<<mine(0, 2, starter, s)<<endl;
string realans = parser(s);
print(realans);
}