-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
88 lines (84 loc) · 2.08 KB
/
program.cpp
File metadata and controls
88 lines (84 loc) · 2.08 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
#include "../include/prevector.h"
#include "hash_map"
#include "map"
using std::map;
using std::hash_map;
const int MAX_10DNA_TYPE = (1 << 20) - 1;
/*
inline int ConvertSubDNAToInt(const string& s, int id = 0, int length = 10)
{
int val = 0;
for (int i = 0; i < length; i++)
val |= (((s[id + 9 - i] & 6) >> 1) << (2 * i));
return val;
}
*/
#define ConvertSubDNAToInt(a, b) int val = 0; \
for (int k = 0; k < 10; k++) val |= (((a[b + 9 - k] & 6) >> 1) << (2 * k));
string ConvertIntToDNAseq(int val, int length = 10)
{
string str(length, 0);
for (int i = length - 1; i != -1; i--)
{
switch (val & 3/*11b*/)
{
case 0:
str[i] = 'A';
break;
case 1:
str[i] = 'C';
break;
case 2:
str[i] = 'G';
break;
case 3:
str[i] = 'T';
break;
}
val >>= 2;
}
return str;
}
#define STD_MAP
vector<string> findRepeatedDnaSequences(string s)
{
// Exceed memory limit
//char* hashTable = new char[MAX_10DNA_TYPE];
//memset(hashTable, 0, MAX_10DNA_TYPE - 100);
#ifdef STD_MAP
map<int, int> hashTable;
#else
#ifdef STD_HASH_MAP
hash_map<int, int> hashTable;
#endif
#endif
vector<string> rst;
if (s.size() < 10)
return rst;
//For each sub string of input string
for (int i = 0; i != s.size() - 9; i++)
{
ConvertSubDNAToInt(s, i);
//cout << val << "\t : " << hashTable[val] << " -> ";
if (hashTable[val] == 2)
continue;
if (++hashTable[val] == 2)
rst.push_back(s.substr(i, 10));
//cout << hashTable[val] << "\n";
}
return rst;
}
int Mymain()
{
TIC
vector<string> testData = {"AAAAAAAAAAA", "", "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", "AAAAACCCCCAAAAACCCCCAAAAAGGGTTT"};
for (string s : testData)
{
cout << s << ": \n";
//findRepeatedDnaSequences(s);
PrintVector(findRepeatedDnaSequences(s), " \n");
//PrintToBinary(ConvertSubDNAToInt(s));
cout << "=========\n";
}
TOC
}