-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
469 lines (445 loc) · 12.1 KB
/
main.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <iostream>
/**
* Review section 1.8 later - June 20, 2017
*/
int main() {
std::cout << -31 % 10 << std::endl;
return 0;
// 00100 = 4
// 00001 = 1
// 10101 = 21
// 00101 = 5
// Set Intersection: (*)
/*
* Often called set multiplication or set addition,
* the operator forms a set containing elements
* belonging to both sets:
*
* {1, 3, 5} * {3, 5, 7} -> {3, 5}
*/
// Set Union: (+)
/**
* Join all members associated with either one, or both
* of the unifying sets.
*
* {1, 3, 5} + {3, 5, 7} -> {1, 3, 5, 7}
*/
// Set Difference: (-)
/**
* Preserves elements in A but not B, if A - B.
*
* {1, 3, 5} - (3, 5, 7} = {1}
* {3, 5, 7} - {1, 3, 5} = {7}
*/
// Symetric Set Difference: (/)
/** Combines all elements found in only one, not
* both, of the sets.
*
* {1, 3, 5} / {3, 5, 7} = {1, 7}
*/
// Set Membership: (IN)
/**
* Denotes that an element is a member of a set.
*
* SET a = {1, 3, 5}
* 1 IN a
* 3 IN a
* 5 IN a
* FALSE -> 7 IN a
*/
/**
* The cardinality of a structured type (in this
* context, the number of possible values that it
* can possess) is equal to the product of the cardinalities
* of its components. Therefore, we can produce a relation:
*
* card(T) = card(T0)^n
*/
/**
* A record is a structured type that, to a programmer, is
* known as a class containing numerous arbitary types/values
* relevant to the stucture. There arbitary types, may,
* themselves, be structures as well. For example:
*
* Person
* ----------------- -----------------
* | String | | Ishac |
* | String | | Mario |
* | Date | --> | 2 | 28 | 2003 | * Notice how this
* | Type (sex) | | Male | is also a
* | Type (martial)| | Single | structured type!
* ----------------- -----------------
*
* A record might not simulate its intended real-life counterpart
* exactly. For example, the Data object permits the 30th of
* February as a valid data due to the nature of its components'
* types, yet this is clearly an invalid date in the real world.
*/
/**
* Random access in computer science refers to the capability
* that each element can be accessed just as easily as the element
* before or after it in the structure.
*
*/
/**
* A sequence does not have a fixed size, as opposed to an array.
* Therefore, dynamic storage allocation must be used. This difference
* (fixed vs dynamic storage allocation) is the line between advanced
* structures and fundamental structures such as arrays, records and
* sets.
*/
/**
* A file provides an oppurtunity to retain the data a program
* generates/produces post-termination. Therefore, the file variable
* in a program is associated with the data on an external device
* through numerous complex operations.
*
* A file is perceived as a sequence, and is read/written to this way,
* rather than directly. This sequence is connected with a rider,
* and the sequential access to the file is determined by the access
* operators involved in the rider.
*/
/**
* New(f, name) - defines f to be the empty sequence.
* Old(f, name) - defines f to be the sequence persistently
* stored with given name.
* Set(r, f, pos) - associate rider r with sequence f, and place
* it at position pos.
* Write(r, x) - place element with value x in the sequence
* designated by rider r, and advance.
* Read(r, x) - assign to x the value of the element designated by
* rider r, and advance.
* Close(f) - registers the written file f in the persistent
* store (flush buffers).
*/
/**
* A buffer serves as a temporary waiting line in order to allow the rider
* to synchronize with the hardware/IO limitations on reading and writing.
* It allows data to be grouped together into blocks due to the waiting times
* enforced.
*
* A buffer is first-in, first-out, and can be represented by the following
* model:
*
* N = buffer size
*
* ----------------------------------------------
* | 1 | 2 | 3 | 4 | 5 | 6 |... | N-1| N |
* ----------------------------------------------
* | 'a'| 'd'| 'e'| '1'| ' '| 'o'| 'q'| '/'| |
* ----------------------------------------------
*
* Deposit(c: char) - insert char at end of buffer if buffer size
* has not been exceeded, otherwise halt
* Fetch() -> c: char - fetch char at current position in buffer
* (note that this position is incremented by one every time
* Fetch() is called)
*/
/**
* A module whose procedure/methods are guaranteed to be executed
* under mutual exclusion is identified as a monitor. All operators on
* shared variables (variabled that are changed/used in various
* procedures throughout the module) are deemed unsafe, and a
* monitor is one solution to this problem.
*/
/**
* The standard and input and output practices we understand revolve
* around the transfer of data from the compter system to external
* agents such as a terminal, LED, etc. The fact that the supplier
* of this data is the computer system makes one thing obvious,
* the data must be transferred into human-readable data.
*/
/**
* A scanner allows the reading of textual input without the knowledge
* of its type, which, unlike writing, is not a necessity. Each scan
* inspects the type and value of the item read. The scanner is similar
* to a rider in the context that it provides a gateway between the input
* (file/text) and the processing of the input.
*/
/**
* Searching methods:
*
/**
* Linear Search:
* Proceeds through the data of the array sequentially until one
* of two conditions is met:
* 1) The element is found, a[i] == x
* 2) The entire array has been scanned, with no match found.
*
* Due to the simplicity of the linear search, there is little chance for
* acceleration from the search. In fact, this little chance occurs from
* a guarantee that the element to be found is at the end of the search.
*
* Below are two examples, encompassing linear search without and with
* the sentinel (the element at the end for the acceleration above) involved.
*
** x is the element to be found
* -------------------------------
* VAR array := ARRAY N of INTEGER
* i := 0
*
* WHILE (i < N) & (array[i] != x)
* INC(i)
* ------------------------------
* VAR array := ARRAY N + 1 of INTEGER
* array[N] := x
* i := 0
*
* WHILE (array[i] != x)
* INC(i)
* ------------------------------
*
* In the second example, if i reached N, then no desired elements were
* found in the array apart from the sentinel.
*/
/**
* Binary Search:
* It is impossible to optimize a search unless further information about
* the data being searched is no. If the data is sorted (greatest -> least,
* least -> greatest, alphabetical, etc.) then we can use a binary search,
* which is faster than a linear search. A binary search uses the
* valid assumption that, since the data is sorted...
*
* If x is greater than a certain element at certain index A, it is also
* greater than all elements located before index A
*
* If x is less thn a certain element at certain index B, it is also
* less than all elements located after index B
*
* The conditions to be met in order for the search to be terminated are
* as follows:
* 1) The element is found, a[m] (the middle of L and R) == x
* 2) L > R (x was not found in the array)
*
* The following examples, for odd and even array lengths, respectively,
* show how L can be > than R if no x is found.
*
* x = 7
* - represents middle
* L represents left boundary
* R represents right boundary
*
* -------------------------
* L RR
* 1 2 3 4 5 6 8 9 10 11 12
* - 7 > 6
* L RR
* 8 9 10 11 12
* -- 7 < 10
* L R
* 8 9
* - 7 < 8
* R L
*
* L > R, element not found
* -------------------------
* L RR
* 1 2 3 4 5 6 8 9 10 11
* - 7 > 5
* L RR
* 6 8 9 10 11
* - 7 < 9
* L R
* 6 8
* - 7 > 6
* L
* R
* 8
* - 7 < 8
* R L
*
* L > R, element not found
* -------------------------
* VAR array := ARRAY N of INTEGER
* L := 0
* R := N - 1
* found := FALSE
*
* WHILE (L <= R) and !found
* m := (L + R) / 2 // Floor
* IF a[m] == x
* found = TRUE
* ELSE
* IF a[m] < x
* L := m + 1
* ELSE
* R := m - 1
* --------------------------
*/
/**
* Straight String Search:
* String searches solve a common scenario faced in text processoing
* systems. This scenario is finding the first appearence of a string/pattern
* B in text A. Thus follows the assumption that the length of A is greater
* than B. In order for string B to be the first occurence of itself in string A,
* it must meet the following two criteria:
* 1) For all chracters in string B,
* A[character index in B + i] = B[character index in B]
* 2) The above criteria is not satisfied for any i less than i
* Where i = index of first character of first B in A
*
* ---------------------------------------
* i := -1
* j := 0
* // M is length of B
* // N is length of A
* REPEAT INC(i):
* j := 0
* WHILE j < M & (A[i+j] == B[j])
* INC(j)
* UNTIL (j == M) OR (i == N - M)
*
* If termination is the result due to j == M, the string has been found
* at index i.
* If termination is the result due to i == N - M, the string has not been
* found.
*/
/**
* The Knuth-Morris-Pratt String Search
* This string search is an improvement on the straight string search,
* as, unlike the straight string search, the KMP string search utilizes
* information found in previous comparisons that is clearly discarded
* in the straight string search.
*
* - -> No Match
* / -> Match
* Hoola-Hooligans
* Hooligan
* / (i = 0, j = 0)
* / (i = 1, j = 1)
* / (i = 2, j = 2)
* / (i = 3, j = 3)
* - (i = 4, j = 4)
* Hooligan (i = 4, j = 0)
* Hooligan (
*
* Hooligan
* Hooligan
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* i
* j
* ABCABD
* ABD
*
* >
* i = 0
* j = 0
*
*
*
*
*
* j := D
* J := j + 1
* j :=
*
*
*
*
*
*
*/
}