forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
anarc09a-1.cpp
33 lines (33 loc) · 971 Bytes
/
anarc09a-1.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
// 2014-04-25
// DP approach (unnecessary! see anarc09a-2.cpp)
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
char str[2001];
int main() {
for (int cs = 1;; cs++) {
scanf("%s", str);
if (str[0] == '-') return 0;
int N = strlen(str);
vector<vector<int> > dp(N + 1, vector<int>(N + 1, 1e6));
dp[0][0] = 0;
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= N; j++) {
if (str[i-1] == '{') {
dp[i][j] = 1 + dp[i-1][j+1];
if (j > 0) {
dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
}
} else {
dp[i][j] = dp[i-1][j+1];
if (j > 0) {
dp[i][j] = min(dp[i][j], 1 + dp[i-1][j-1]);
}
}
}
}
printf("%d. %d\n", cs, dp[N][0]);
}
}