제출 #256385

#제출 시각아이디문제언어결과실행 시간메모리
256385shayan_pVim (BOI13_vim)C++14
75.61 / 100
2121 ms524292 KiB
// And you curse yourself for things you never done

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn = 70010, mod = 1e9 + 7, inf = 1e9 + 10;

int nxt[maxn][10], lst[10];

struct Hash{
    int operator() (const pii &p)const{
	return hash<ll>()( 1ll * p.F * maxn + p.S );
    }
};

unordered_map<pii, int, Hash> dp;
vector<int> poses;

int solve(int i, int j){
    if(i >= sz(poses))
	return 0;
    if(dp.count({i, j}))
	return dp[{i, j}];
    int ans = inf;
    if(j >= poses[i])
	ans =  j - poses[i] + solve( upper_bound(poses.begin(), poses.end(), j) - poses.begin(), poses[i] );
    if(nxt[ poses[i] ][8] + 10 < j)
	return dp[{i, j}] = ans;
    int pt = lower_bound(nxt[j], nxt[j] + 10, poses[i]) - nxt[j];
    for(int w = 8 ; w >= 0 && nxt[j][w] != j && w >= pt-1; w--)
	ans = min(ans, 2 + solve(i, nxt[j][w]));
    return dp[{i, j}] = ans;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();

    dp.reserve(1<<17);
    dp.max_load_factor(0.25);
    
    int n;
    cin >> n;
    string _s, s;
    cin >> _s;
    int ans = 0;
    for(int i = 0; i < n; i++){
	if(_s[i] == 'e')
	    ans+= 2;
	if(_s[i] == 'e' && _s[i + 1] != 'e')
	    poses.PB(sz(s));
	if(_s[i] != 'e')
	    s+= _s[i];	
    }
    memset(lst, -1, sizeof lst);
    memset(nxt, -1, sizeof nxt);
    n = sz(s);
    for(int i = n-1; i >= 0; i--){
	for(int j = 0; j < 9; j++)
	    nxt[i][j] = (lst[j] == -1 ? i : lst[j]);
	sort(nxt[i], nxt[i] + 9);
	nxt[i][9] = inf;
	int id = (s[i] - 'a') - (s[i] > 'e');
	lst[id] = i;
    }
    ans+= solve(0, 0);
    return cout << ans << "\n", 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...