-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path187.cpp
More file actions
53 lines (49 loc) · 1.34 KB
/
187.cpp
File metadata and controls
53 lines (49 loc) · 1.34 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
//
// 187.cpp
// LeetCode
//
// Created by 张佐玮 on 15/8/9.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Repeated DNA Sequences
//
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> results;
unsigned int eraser20 = 0xFFFFF;
unsigned int current = 0;
if (s.size() < 11) {
return results;
}
unordered_set<int> unique, dnaResult;
unordered_map<char, int> dna{{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};
for (int i = 0; i < 10; i++) {
current = (current << 2) + dna[s[i]];
}
unique.insert(current);
for (int i = 10; i < s.size(); i++) {
current = ((current << 2) + dna[s[i]]) & eraser20;
if (!unique.insert(current).second && dnaResult.insert(current).second) {
results.push_back(s.substr(i - 9, 10));
}
}
return results;
}
};
class Test {
public:
void sample() {
string s("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT");
Solution solution;
vector<string> result = solution.findRepeatedDnaSequences(s);
for (auto &str: result) {
cout << str << endl;
}
}
};