#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int dp[5050][5050], nxt[10][5050], prv[5050];
vector<int> E, pos;
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n;
string a;
cin >> n >> a;
if (n>5000) {printf("0\n"); return 0;}
string::iterator s = a.begin()-1;
int pt[10] = {0};
E.push_back(0);
for (int i=1;i<=n;i++){
if (s[i]=='e'){
E.push_back(i);
prv[i] = (int)E.size()-1;
}
else{
while(pos.size()<E.size()) pos.push_back(i);
prv[i] = prv[i-1];
for (int j=pt[s[i]-'a'];j<i;j++) nxt[s[i]-'a'][j] = i;
pt[s[i]-'a'] = i;
}
}
for (int j=0;j<10;j++){
for (int i=pt[j];i<=n;i++) nxt[j][i] = 1e9;
//for (int i=1;i<=n;i++) printf("%d ", nxt[j][i]);
//printf("\n");
}
/*for (auto &x:E) printf("%d ", x);
printf("\n");
for (auto &x:pos) printf("%d ", x);
printf("\n");
for (int i=1;i<=n;i++) printf("%d ", prv[i]);
printf("\n");*/
if (E.empty()){
printf("0\n"); return 0;
}
for (int i=0;i<(int)E.size();i++){
fill(dp[i], dp[i]+n+1, 1e9);
}
dp[0][1] = 0;
for (int i=0;i<(int)E.size();i++){
for (int j=1;j<=n;j++){
for (int k=0;k<10;k++){
int idx = nxt[k][j];
if (idx==1e9) continue;
if (prv[idx]>i) dp[prv[idx]][pos[i+1]] = min(dp[prv[idx]][pos[i+1]], dp[i][j] + (idx - E[i+1]) + (prv[idx] - i) + 2);
dp[i][idx] = min(dp[i][idx], dp[i][j] + 2);
}
}
}
//printf("%d\n", nxt[3][pos[0]]);
//printf("%d\n", dp[0][0] + (6 - E[0]));
int ans = 1e9;
for (int j=1;j<=n;j++) ans = min(ans, dp[(int)E.size()-1][j]);
printf("%d\n", ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2008 KB |
Output is correct |
2 |
Correct |
3 ms |
1356 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
3 ms |
1228 KB |
Output is correct |
5 |
Correct |
4 ms |
1356 KB |
Output is correct |
6 |
Correct |
6 ms |
1796 KB |
Output is correct |
7 |
Correct |
5 ms |
1612 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
8 ms |
2012 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
3 ms |
1356 KB |
Output is correct |
17 |
Correct |
4 ms |
1228 KB |
Output is correct |
18 |
Correct |
3 ms |
1228 KB |
Output is correct |
19 |
Correct |
3 ms |
1228 KB |
Output is correct |
20 |
Correct |
5 ms |
1228 KB |
Output is correct |
21 |
Correct |
4 ms |
1228 KB |
Output is correct |
22 |
Correct |
4 ms |
1412 KB |
Output is correct |
23 |
Correct |
2 ms |
972 KB |
Output is correct |
24 |
Correct |
3 ms |
972 KB |
Output is correct |
25 |
Correct |
2 ms |
972 KB |
Output is correct |
26 |
Correct |
3 ms |
1100 KB |
Output is correct |
27 |
Correct |
5 ms |
1512 KB |
Output is correct |
28 |
Correct |
6 ms |
1740 KB |
Output is correct |
29 |
Correct |
5 ms |
1612 KB |
Output is correct |
30 |
Correct |
3 ms |
1100 KB |
Output is correct |
31 |
Correct |
3 ms |
972 KB |
Output is correct |
32 |
Correct |
3 ms |
1100 KB |
Output is correct |
33 |
Correct |
3 ms |
1100 KB |
Output is correct |
34 |
Correct |
5 ms |
1356 KB |
Output is correct |
35 |
Correct |
5 ms |
1484 KB |
Output is correct |
36 |
Correct |
6 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
735 ms |
61564 KB |
Output is correct |
2 |
Correct |
579 ms |
47104 KB |
Output is correct |
3 |
Correct |
210 ms |
22348 KB |
Output is correct |
4 |
Correct |
734 ms |
61568 KB |
Output is correct |
5 |
Correct |
524 ms |
43680 KB |
Output is correct |
6 |
Correct |
372 ms |
27568 KB |
Output is correct |
7 |
Correct |
573 ms |
52896 KB |
Output is correct |
8 |
Correct |
604 ms |
51192 KB |
Output is correct |
9 |
Correct |
581 ms |
47104 KB |
Output is correct |
10 |
Correct |
472 ms |
44192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
472 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |