FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learning-notes/algorithm/adversarial_input/prog2.cpp at master · ifding/learning-notes · GitHub
ifding
/
learning-notes
Public
Notifications
You must be signed in to change notification settings
Fork
19
Star
63
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
learning-notes
/
algorithm
/
adversarial_input
/
prog2.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
67 lines (55 loc) · 1.34 KB
Breadcrumbs
learning-notes
/
algorithm
/
adversarial_input
/
prog2.cpp
Copy path
File metadata and controls
executable file
·
67 lines (55 loc) · 1.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
iostream
>
#
include
<
algorithm
>
using
namespace
std
;
//
Find number of distinct elements using a hash table
struct
Node
{
int
val;
Node *next;
Node
(
int
v, Node *n) { val = v; next = n; }
};
Node **T;
//
array of Node *s for hash table
int
table_size =
10000
;
//
check out this awesome hash function!
int
hash
(
int
x)
{
return
((
unsigned
int
)x *
3
+
17
) % table_size;
}
bool
find
(
int
x)
{
for
(Node *n = T[
hash
(x)]; n !=
NULL
; n = n->
next
)
if
(n->
val
== x)
return
true
;
return
false
;
}
void
insert
(
int
x)
{
int
index =
hash
(x);
T[index] =
new
Node
(x, T[index]);
}
int
main
(
void
)
{
int
N;
//
size of input
if
(!(cin >> N) || N <
1
|| N >
100000
) {
cout <<
"
Invalid input size!
\n
"
;
return
0
;
}
//
read input
int
*A =
new
int
[N];
for
(
int
i=
0
; i<N; i++)
if
(!(cin >> A[i]) || A[i]<
0
) {
cout <<
"
Invalid input!
\n
"
;
return
0
;
}
//
initialize hash table -- if I make it have size 10,000, then the
//
average length of a linked list should be small...
T =
new
Node *[table_size];
for
(
int
i=
0
; i<table_size; i++)
T[i] =
NULL
;
int
count =
0
;
for
(
int
i=
0
; i<N; i++)
if
(!
find
(A[i])) {
count++;
insert
(A[i]);
}
cout <<
"
The input contains
"
<< count <<
"
distinct integers.
\n
"
;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL