This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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, e=0;
string s;
cin >> n >> s;
rep(i, n) {
if(s[i]=='e') {
s[i]='a';
++e;
} else if(s[i]=='a') s[i]='e';
}
string xd="";
xd+=s[0];
for(int i=1; i<n; ++i) if(xd.back()!='a' || s[i]!='a') xd+=s[i];
s=xd;
n=s.size();
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) {
cout << o+e << '\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 c=i;
if(nxt[p][0]+1==nxt[a][i]) c=0;
if(odl[nxt[p][0]+1][c]==INF) q.push({-o-2-nxt[a][i]+nxt[p][0], {nxt[p][0]+1, c}});
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |