-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon_shared_substring.cpp
145 lines (118 loc) · 2.22 KB
/
non_shared_substring.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
#define LETTERS_NUM 6
struct Node {
Node () :
letter(' '),
is_text1(false),
is_text2(false),
prev(-1)
{
for (int i = 0; i < LETTERS_NUM; i++) {
next[i] = -1;
}
}
char letter;
bool is_text1;
bool is_text2;
int prev;
int next[LETTERS_NUM];
};
typedef vector<Node> Trie;
static int letter_to_index(char letter)
{
switch (letter) {
case 'A':
return 0;
case 'C':
return 1;
case 'T':
return 2;
case 'G':
return 3;
case '#':
return 4;
case '$':
return 5;
default:
assert(0);
return -1;
}
}
static Trie create_suffix_trie(const string &text)
{
Trie trie(1);
bool is_text1 = true;
bool is_text2 = false;
for (size_t i = 0; i < text.size(); i++) {
int v = 0;
for (size_t j = i; j < text.size(); j++) {
int index = letter_to_index(text[j]);
int w = trie[v].next[index];
if (w == -1) {
w = trie.size();
trie.push_back(Node());
trie[v].next[letter_to_index(text[j])] = w;
trie[w].letter = text[j];
trie[w].prev = v;
}
if (is_text1) trie[w].is_text1 = true;
if (is_text2) trie[w].is_text2 = true;
v = w;
}
if (text[i] == '#') {
is_text1 = false;
is_text2 = true;
}
}
return trie;
}
static string solve(const string &text1, const string &text2)
{
string text = text1 + "#" + text2 + "$";
Trie trie = create_suffix_trie(text);
queue<int> q;
vector<bool> visited(trie.size(), false);
int w = -1;
q.push(0);
visited[0] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
if (trie[v].letter == '#') continue;
if (trie[v].letter == '$') continue;
if (trie[v].is_text1 && !trie[v].is_text2) {
w = v;
break;
}
for (int i = 0; i < LETTERS_NUM; i++) {
int z = trie[v].next[i];
if (z != -1 && !visited[z]) {
q.push(z);
visited[z] = true;
}
}
}
if (w == -1) return text1;
string result;
while (w != 0) {
result += trie[w].letter;
w = trie[w].prev;
}
reverse(result.begin(), result.end());
return result;
}
int main(void)
{
string text1, text2;
cin >> text1;
cin >> text2;
cout << solve(text1, text2) << endl;
return 0;
}