-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cpp
More file actions
97 lines (85 loc) · 1.96 KB
/
dictionary.cpp
File metadata and controls
97 lines (85 loc) · 1.96 KB
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
#include <bits/stdc++.h>
#define MAX 200023
#define M 200017
int hash1(int key) {
return 1 + key%(M-1);
}
int hash2(int key) {
return key%M;
}
int hash(int key, int i) {
return (hash1(key) + i*hash2(key)) % M;
}
void insert(std::array<int, MAX>& A, int key) {
int j, i=0;
while(true) {
j = hash(key, i);
if (A[j] == 0) {
A[j] = key;
return;
} else {
i++;
}
}
}
int ConvertNumFromString(char s) {
if (s == 'A') {
return 1;
} else if (s == 'C') {
return 2;
} else if (s == 'G') {
return 3;
} else if (s == 'T') {
return 4;
} else {
return -1;
}
}
int RetriveNumFromString(std::string str) {
char s[3];
int num = 0;
for (int i=0; i<str.length(); i++) {
s[i] = str[i];
num += ConvertNumFromString(s[i])*std::pow(10, str.length()-i-1);
}
return num;
}
bool find(std::array<int, MAX> A, int key) {
int j, i=0;
while(true) {
j = hash(key, i);
if (A[j] == key) {
return true;
} else if(A[j] == 0 || i>=M) {
return false;
} else {
i++;
}
}
}
int main()
{
int n, num_chr;
std::array<int, MAX> Array{0 };
// 前方一致の正規表現
std::regex re_insert(R"(^insert.*$)") ;
std::regex re_find(R"(^find.*$)");
// 命令数の取得
std::cin >> n;
std::string s,num_ins, num_find, result;
for (int i = 0; i < n+1; i++)
{
getline(std::cin,s);
if (std::regex_match(s, re_insert)) {
num_ins = s.substr(7);
num_chr = RetriveNumFromString(num_ins);
insert(Array, num_chr);
} else if(std::regex_match(s, re_find)) {
num_ins = s.substr(5);
num_chr = RetriveNumFromString(num_ins);
result = find(Array, num_chr)? "yes" : "no";
std::cout << result << "\n";
}
}
return 0;
}