-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_bitwise_n.cpp
65 lines (55 loc) · 1.5 KB
/
AC_bitwise_n.cpp
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_bitwise_n.cpp
* Create Date: 2015-02-25 11:38:33
* Descripton: Modified from nlogn version. Use vector<bool>.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
int get_value(char ch) {
switch (ch) {
case 'A': return 0; break;
case 'C': return 1; break;
case 'G': return 2; break;
case 'T': return 3; break;
}
}
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<bool> appeared(1100000), used(1100000);
vector<string> ans;
if (s.length() <= 10)
return ans;
int hash = 0;
// get first substring's hash
for (int i = 0; i < 10; i++)
hash = (hash << 2) | get_value(s[i]);
appeared[hash] = true;
// deal every substring
for (int i = 10; i < s.length(); ++i) {
hash = ((1<<20) - 1) & ((hash << 2) | get_value(s[i]));
if (used[hash])
continue;
if (appeared[hash]) {
ans.push_back(s.substr(i - 9, 10));
used[hash] = true;
} else
appeared[hash] = true;
}
return ans;
}
};
int main() {
string ss;
Solution s;
while (cin >> ss) {
vector<string> ans = s.findRepeatedDnaSequences(ss);
for (auto &i : ans)
cout << i << ", ";
cout << endl;
}
return 0;
}