-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK_dna_1.cpp
More file actions
66 lines (56 loc) · 1.41 KB
/
K_dna_1.cpp
File metadata and controls
66 lines (56 loc) · 1.41 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
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;
const long long hashPoint = 997;
struct Hasher {
vector<long long> h;
vector<long long> p;
Hasher(const string& s) {
int n = s.length();
h.resize(n + 1);
p.resize(n + 1);
p[0] = 1;
for (int i = 0; i < n; ++i) {
h[i + 1] = h[i] * hashPoint + s[i];
p[i + 1] = p[i] * hashPoint;
}
}
long long get(int l, int r) {
return h[r] - h[l] * p[r - l];
}
};
int main() {
int N, M;
cin >> N >> M;
string s, t;
cin >> s >> t;
set<long long> substrings;
for (int i = 0; i < t.length(); ++i) {
long long h = 0;
for (int j = i; j < t.length(); ++j) {
h = h * hashPoint + t[j];
substrings.insert(h);
}
}
int ansl = 0, ansr = s.length() + 1;
Hasher h(s);
int r = 0;
for (int i = 0; i < s.length(); ++i) {
r = max(i, r);
while (r < s.length() && substrings.count(h.get(i, r + 1)) > 0) {
r++;
}
if (r < s.length() && (ansr - ansl > r + 1 - i)) {
ansl = i;
ansr = r + 1;
}
}
if (ansr == s.length() + 1) {
cout << "NONE\n";
} else {
cout << s.substr(ansl, ansr - ansl) << '\n';
}
return 0;
}