-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmate.cpp
114 lines (90 loc) · 2.11 KB
/
mate.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
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (vector<pair<ii, int> >::iterator it = c.begin(); it != c.end(); it++)
string str;
int type;
int dp[10010][3];
vector<int> nex[2];
ii getLength() {
nex[0].clear();
nex[0].resize(str.size(), -1);
nex[1].clear();
nex[1].resize(str.size(), -1);
memset(dp, -1, sizeof(dp));
for(int i = 0; i < str.size(); i++) {
dp[i][0] = 1;
dp[i][1] = 1;
}
for(int i = str.size() - 1; i >= 0; i--) {
//dp[i][0] >
for(int j = i + 1; j < str.size(); j++) {
if(str[i] < str[j]) {
if(dp[i][0] < 1 + dp[j][1]) {
dp[i][0] = 1 + dp[j][1];
nex[0][i] = j;
}
}
}
//dp[i][1] <
for(int j = i + 1; j < str.size(); j++) {
if(str[i] > str[j]) {
if(dp[i][1] < 1 + dp[j][0]) {
dp[i][1] = 1 + dp[j][0];
nex[1][i] = j;
}
}
}
}
int result = INT_MIN;
int index, sign;
for(int i = 0; i < str.size(); i++) {
if(dp[i][0] > result) {
result = dp[i][0];
index = i;
sign = 0;
}
if(dp[i][1] > result) {
result = dp[i][1];
index = i;
sign = 1;
}
}
cout << result << endl;
return make_pair(index, sign);
}
void reconstruct(ii start) {
int index = start.first;
int sign = start.second;
while(true) {
cout << str[index];
if(nex[sign][index] == -1) {
break;
}
index = nex[sign][index];
sign = 1 - sign;
}
cout << endl;
}
int main() {
cin >> str;
cin >> type;
ii start = getLength();
if(type == 1) {
reconstruct(start);
}
return 0;
}