-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLCS.cpp
216 lines (211 loc) · 5.11 KB
/
MLCS.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <time.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 150000000 + 5;
const int mm = 150000000 + 4;
int n, length;
clock_t start_time, end_time;
int cmap[256], ans;
vector<int> OD(MAXN, 0), ID(MAXN, 0), result(MAXN, 0), level(MAXN, 0);
vector<bool> vis(MAXN, false);
string s[3005];
vector<int> ST[3005][5];
vector<vector<int> > G(MAXN), pre(MAXN), Set(MAXN);
map<vector<int>, int> mp;
double tot;
int maxcnt, id;
queue<int> q;
struct cmp1 {
bool operator()(int a, int b) { return ID[a] > ID[b]; }
};
void Init() {
mp.clear();
cmap['A'] = 1;
cmap['T'] = 2;
cmap['C'] = 3;
cmap['G'] = 4;
maxcnt = 0;
id = 0;
}
void Construct_ST() {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < length; j++) {
int d = ST[i][cmap[s[i][j]]].size();
for (int k = d; k <= j; k++) {
ST[i][cmap[s[i][j]]].push_back(j + 1);
}
}
}
}
void Construct() {
cout << "start Construct" << endl;
q.push(1);
for (int i = 0; i < n; i++) {
Set[1].push_back(0);
Set[mm].push_back(mm);
}
mp[Set[1]] = 1;
ans = 2;
int flag = 0, number = 0;
int p;
while (!q.empty()) {
p = q.front();
q.pop();
if (vis[p]) {
continue;
}
vis[p] = true;
number = 0;
flag = 0;
for (int i = 1; i <= 4; i++) {
flag = 0;
for (int j = 1; j <= n; j++) {
if (ST[j][i].size() > Set[p][j - 1]) {
Set[ans].push_back(ST[j][i][Set[p][j - 1]]);
} else {
flag = 1;
break;
}
}
if (flag == 0) {
int ddt;
if (mp[Set[ans]] == 0) {
mp[Set[ans]] = ans;
ddt = ans;
ans++;
q.push(ddt);
} else {
ddt = mp[Set[ans]];
Set[ans].clear();
}
G[p].push_back(ddt);
ID[ddt]++;
OD[p]++;
number++;
} else {
Set[ans].clear();
}
}
if (number == 0) {
if (p != mm) {
G[p].push_back(mm);
OD[p]++;
ID[mm]++;
}
}
}
cout << ans << endl;
cout << "over Construct" << endl;
}
void ForwardTopSort() {
cout << "start ForwardTopSort" << endl;
maxcnt = 0;
q.push(1);
level[1] = 1;
int p;
while (!q.empty()) {
p = q.front();
q.pop();
int d = G[p].size();
if (p != mm) maxcnt = max(maxcnt, level[p]);
for (int i = 0; i < d; i++) {
ID[G[p][i]]--;
pre[G[p][i]].push_back(p);
if (ID[G[p][i]] == 0) {
q.push(G[p][i]);
level[G[p][i]] = level[p] + 1;
}
}
G[p].clear();
}
cout << "over ForwardTopSort" << endl;
}
void BackwardTopSort() {
cout << "start BackwardTopSort" << endl;
int p;
level[mm] = maxcnt + 1;
q.push(mm);
while (!q.empty()) {
p = q.front();
q.pop();
if (vis[p]) {
continue;
}
vis[p] = true;
int d = pre[p].size();
int nowlevel = level[p];
for (int i = 0; i < d; i++) {
if (level[pre[p][i]] == nowlevel - 1) {
q.push(pre[p][i]);
G[pre[p][i]].push_back(p);
}
}
}
cout << "over BackwardTopSort" << endl;
}
void dfs(int x, int number) {
int d = G[x].size();
if (x == mm) {
id++;
cout << "The " << id << " mlcs is : ";
for (int i = 0; i < number - 1; i++) {
cout << s[1][Set[result[i]][0] - 1];
}
cout << "\n";
return;
}
for (int i = 0; i < G[x].size(); i++) {
result[number] = G[x][i];
dfs(G[x][i], number + 1);
}
}
void Output() {
cout << "length of mlcs is : " << maxcnt - 1 << endl;
dfs(1, 0);
}
void TOP_MLCS() {
clock_t n1 = clock();
double p = 0;
Construct_ST();
clock_t n2 = clock();
p = ((double)(n2 - n1)) / CLOCKS_PER_SEC;
tot += p;
cout << " Construct_ST time : " << p << "\n";
Construct();
n1 = clock();
p = ((double)(n1 - n2)) / CLOCKS_PER_SEC;
tot += p;
cout << "Construct time : " << p << "\n";
ForwardTopSort();
n2 = clock();
p = ((double)(n2 - n1)) / CLOCKS_PER_SEC;
tot += p;
cout << "ForwardTopSort time :" << p << "\n";
vis = vector<bool>(MAXN, false);
n2 = clock();
BackwardTopSort();
n1 = clock();
p = ((double)(n1 - n2)) / CLOCKS_PER_SEC;
tot += p;
cout << "BackwardTopSort time :" << p << "\n";
Output();
}
int main() {
Init();
n = 0;
tot = 0;
while (cin >> s[n + 1]) {
++n;
}
cout << n << endl;
length = s[1].size();
TOP_MLCS();
cout << "total time : " << tot << "\n";
cout << maxcnt - 1 << endl;
return 0;
}