-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetermining DNA Health
84 lines (80 loc) · 1.97 KB
/
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
#include <bits/stdc++.h>
using namespace std;
int aInt = 'a';
int health[100000];
struct trie {
struct trie* next[26];
vector<int> term;
trie() {
for (int i = 0; i < 26 ; i++) {
next[i] = NULL;
}
}
};
trie *root;
void insert (char stringArray[],int index){
trie *currentNode=root;
for(int i=0; i<strlen(stringArray);i++){
int idx = stringArray[i] -aInt;
if (currentNode->next[idx] == NULL) {
currentNode->next[idx] = new trie();
}
currentNode = currentNode->next[idx];
}
currentNode->term.push_back(index);
}
long calculateTotalHealth (char stringArray[],int beg, int end){
struct trie *currentNode;
long totalHealth=0;
int increaseString;
long stringLength=strlen(stringArray);
for(int i=0;i<stringLength;i++){
increaseString=i;
currentNode = root->next[stringArray[i] - aInt];
while (currentNode != NULL && stringLength > increaseString) {
if (!(currentNode->term.empty())) {
for (auto k : currentNode->term) {
if(k>=beg && k<=end){
totalHealth+=health[k];
}
}
}
currentNode =
currentNode->next[stringArray[increaseString + 1] - aInt];
increaseString++;
}
}
return totalHealth;
}
int main()
{
root= new trie();
char allStringsTogether[2000001];
int n,s;
scanf("%d",&n);
for(int i=0; i<n ; i++){
scanf("%s",allStringsTogether);
insert(allStringsTogether,i);
}
for ( int i=0; i<n ; i++){
scanf("%d",&health[i]);
}
scanf("%d",&s);
int genesStart,genesEnd;
long long totalHealth=0;
long long min=20000000000000;
long long max=-1;
for(int i=0; i<s;i++){
scanf("%d%d%s",&genesStart,&genesEnd,allStringsTogether);
totalHealth=calculateTotalHealth(allStringsTogether,genesStart,genesEnd);
if(totalHealth>max){
max=totalHealth;
}
if(totalHealth<min){
min=totalHealth;
}
totalHealth=0;
}
cout<<min<<" "<<max;
return 0;
}