zoukankan      html  css  js  c++  java
  • C. Remove Adjacent

    You are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s||s|. You may perform several operations on this string.

    In one operation, you can choose some index ii and remove the ii-th character of ss (sisi) if at least one of its adjacent characters is the previous letter in the Latin alphabet for sisi. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index ii should satisfy the condition 1i|s|1≤i≤|s| during each operation.

    For the character sisi adjacent characters are si1si−1 and si+1si+1. The first and the last characters of ss both have only one adjacent character (unless |s|=1|s|=1).

    Consider the following example. Let s=s= bacabcab.

    1. During the first move, you can remove the first character s1=s1= b because s2=s2= a. Then the string becomes s=s= acabcab.
    2. During the second move, you can remove the fifth character s5=s5= c because s4=s4= b. Then the string becomes s=s= acabab.
    3. During the third move, you can remove the sixth character s6=s6='b' because s5=s5= a. Then the string becomes s=s= acaba.
    4. During the fourth move, the only character you can remove is s4=s4= b, because s3=s3= a (or s5=s5= a). The string becomes s=s= acaa and you cannot do anything with it.

    Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.

    Input

    The first line of the input contains one integer |s||s| (1|s|1001≤|s|≤100) — the length of ss.

    The second line of the input contains one string ss consisting of |s||s| lowercase Latin letters.

    Output

    Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.

    Examples
    input
    Copy
    8
    bacabcab
    
    output
    Copy
    4
    
    input
    Copy
    4
    bcda
    
    output
    Copy
    3
    
    input
    Copy
    6
    abbbbb
    
    output
    Copy
    5
    
    Note

    The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is 44.

    In the second example, you can remove all but one character of ss. The only possible answer follows.

    1. During the first move, remove the third character s3=s3= d, ss becomes bca.
    2. During the second move, remove the second character s2=s2= c, ss becomes ba.
    3. And during the third move, remove the first character s1=s1= b, ss becomes a.

     

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int N = 30005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    int main()
    {
        int n;
        cin >> n;
        string s;
        cin >> s;
        bool fg=1;
        int ans = 0;
        for (char c = 'z'; c >= 'a'; c--)
            for (int i = 0; i<s.size(); i++)
                if (s[i] == c && (i>0 && s[i - 1] == c - 1 || i<s.size() - 1 && s[i + 1] == c - 1))
                    s.erase(s.begin() + i), i = -1, ans++;
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    HDU 2047 阿牛的EOF牛肉串
    HDU 2015 偶数求和
    HDU 2029 算菜价
    HDU 2028 Lowest Common Multiple Plus
    动态函数库设计
    静态函数库设计
    Linux编程规范
    Linux应用程序地址布局
    Core Dump 程序故障分析
    W-D-S-UART编程
  • 原文地址:https://www.cnblogs.com/dealer/p/12763955.html
Copyright © 2011-2022 走看看