#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=7e4+7, INF=1e9+7;
int nxt[LIM][10], odl[LIM][10], kiedy[10];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
string s;
cin >> n >> s;
rep(i, n) {
if(s[i]=='e') s[i]='a';
else if(s[i]=='a') s[i]='e';
}
rep(i, 10) kiedy[i]=INF;
for(int i=n-1; i>=0; --i) {
rep(j, 10) {
nxt[i][j]=kiedy[j];
odl[i][j]=INF;
}
kiedy[s[i]-'a']=i;
}
priority_queue<pair<int,pair<int,int>>>q;
q.push({0, {0, 0}});
while(!q.empty()) {
int o=-q.top().st, a=q.top().nd.st, b=q.top().nd.nd; q.pop();
if(o>=odl[a][b]) continue;
odl[a][b]=o;
int p=nxt[a][b];
if(!b) p=a;
if(nxt[p][0]==INF) {
rep(i, n) if(s[i]=='a') ++o;
cout << o << '\n';
break;
}
for(int i=1; i<10; ++i) if(nxt[a][i]<INF && nxt[a][i]>=p) {
if(nxt[p][0]>nxt[a][i]) {
if(odl[nxt[a][i]][i]==INF) q.push({-o-2, {nxt[a][i], 0}});
} else {
int mi=INF, c=i;
for(int j=1; j<10; ++j) mi=min(mi, nxt[nxt[p][0]][j]);
if(nxt[a][i]==mi) c=0;
if(odl[mi][c]==INF) q.push({-o-2-nxt[a][i]+nxt[p][0], {mi, c}});
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
324 KB |
Output is correct |
29 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Incorrect |
1 ms |
376 KB |
Output isn't correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
716 KB |
Output is correct |
2 |
Correct |
18 ms |
764 KB |
Output is correct |
3 |
Correct |
7 ms |
584 KB |
Output is correct |
4 |
Correct |
12 ms |
712 KB |
Output is correct |
5 |
Incorrect |
16 ms |
972 KB |
Output isn't correct |
6 |
Incorrect |
11 ms |
1172 KB |
Output isn't correct |
7 |
Incorrect |
11 ms |
904 KB |
Output isn't correct |
8 |
Incorrect |
12 ms |
716 KB |
Output isn't correct |
9 |
Correct |
18 ms |
760 KB |
Output is correct |
10 |
Incorrect |
13 ms |
756 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
166 ms |
8404 KB |
Output isn't correct |
2 |
Incorrect |
105 ms |
5100 KB |
Output isn't correct |
3 |
Incorrect |
146 ms |
5228 KB |
Output isn't correct |
4 |
Incorrect |
119 ms |
7668 KB |
Output isn't correct |
5 |
Incorrect |
184 ms |
9112 KB |
Output isn't correct |
6 |
Incorrect |
135 ms |
5296 KB |
Output isn't correct |
7 |
Incorrect |
213 ms |
8776 KB |
Output isn't correct |
8 |
Incorrect |
170 ms |
8764 KB |
Output isn't correct |
9 |
Incorrect |
176 ms |
7196 KB |
Output isn't correct |
10 |
Incorrect |
158 ms |
5580 KB |
Output isn't correct |
11 |
Incorrect |
123 ms |
7688 KB |
Output isn't correct |
12 |
Incorrect |
227 ms |
9204 KB |
Output isn't correct |
13 |
Incorrect |
130 ms |
5836 KB |
Output isn't correct |
14 |
Incorrect |
103 ms |
5836 KB |
Output isn't correct |
15 |
Incorrect |
209 ms |
5968 KB |
Output isn't correct |
16 |
Incorrect |
110 ms |
6804 KB |
Output isn't correct |
17 |
Incorrect |
165 ms |
8448 KB |
Output isn't correct |
18 |
Incorrect |
112 ms |
5316 KB |
Output isn't correct |
19 |
Incorrect |
95 ms |
5116 KB |
Output isn't correct |
20 |
Incorrect |
185 ms |
5072 KB |
Output isn't correct |