[Hacker Rank] Determining DNA Health

Determining DNA Health

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
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

struct Trie {
Trie* next[26];
vector<pair<int,int>> healths;
Trie () {memset(next,0,sizeof(next));}
void insert(int& index, int& health, int p, string& s) {
if(s.length() == p) {
healths.push_back({index, health});
} else {
if(!next[s[p]-'a']) next[s[p]-'a'] = new Trie();
next[s[p]-'a']->insert(index, health, p + 1, s);
}
}
long helper(int from, int to) {
long res = 0;
auto it = lower_bound(healths.begin(), healths.end(), make_pair(from, -1));
if(it == end(healths) or it->first > to) return res;
for(; it != end(healths) and it->first <= to; it++) {
res += it->second;
}
return res;
}
long qry(int from, int to, string& s, int p) {
long res = helper(from, to);
if(p == s.length() or !next[s[p]-'a']) return res;
return res += next[s[p]-'a']->qry(from,to,s,p+1);
}
};

int main()
{
string n_temp;
getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));

string genes_temp_temp;
getline(cin, genes_temp_temp);

vector<string> genes_temp = split(rtrim(genes_temp_temp));

vector<string> genes(n);

for (int i = 0; i < n; i++) {
string genes_item = genes_temp[i];

genes[i] = genes_item;
}

string health_temp_temp;
getline(cin, health_temp_temp);

vector<string> health_temp = split(rtrim(health_temp_temp));

vector<int> health(n);

for (int i = 0; i < n; i++) {
int health_item = stoi(health_temp[i]);

health[i] = health_item;
}

Trie* trie = new Trie();
for(int i = 0; i < n; i++) {
trie->insert(i, health[i], 0, genes[i]);
}

string s_temp;
getline(cin, s_temp);

int s = stoi(ltrim(rtrim(s_temp)));

long mi = LONG_MAX, ma = LONG_MIN;

for (int s_itr = 0; s_itr < s; s_itr++) {
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);

vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

int first = stoi(first_multiple_input[0]);

int last = stoi(first_multiple_input[1]);

string d = first_multiple_input[2];
long res = 0;
for(int i = 0; i < d.length(); i++) {
res += trie->qry(first,last,d,i);
}
mi = min(mi, res);
ma = max(ma, res);
}

cout<<mi<<" "<<ma<<endl;
return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

vector<string> split(const string &str) {
vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

return tokens;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/01/PS/HackerRank/determining-dna-health/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.