Codeforces 1321C Remove Adjacent

题目链接

2s+n=100,可以直接暴力贪心,从末尾z开始删到a即可

#include<bits/stdc++.h>
using namespace std;
#define ms(x,y) memset(x, y, sizeof(x))
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;


void run_case() {
    int n;
    string str;
    cin >> n >> str;
    int ans = 0;
    for(int k = 25; k >= 0; --k) {
        for(int t = 0; t < 100; ++t) {
            for(int i = 0; i < str.size(); ++i) {
                if((str[i] - 'a' == k) && ((str[i-1] - 'a' == k-1) || (str[i+1] - 'a' == k-1))) {
                    ans++;
                    str.erase(i, 1);
                    i--;
                }
            }
        }
    }
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(2);
    //int t; cin >> t;
    //while(t--)
    run_case();
    cout.flush();
    return 0;
}
posted @ 2020-03-08 15:47  GRedComeT  阅读(98)  评论(0编辑  收藏  举报