zoukankan      html  css  js  c++  java
  • Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) 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
     
    8
    bacabcab
    
    Output
     
    4
    
    Input
     
    4
    bcda
    
    Output
     
    3
    
    Input
     
    6
    abbbbb
    
    Output
     
    5
    大意是如果字符串中左右两边的字符比当前字符小1的话就可以删掉当前字符,问删除操作最多可以有多少次。可以想到,删除一个大的字符后剩余字符的删除情况肯定不会变得更少,所以贪心的先从较大的数处理。注意到n很小,可以直接O(n^3)暴力,vector的erase处理。
    #include <bits/stdc++.h>
    using namespace std;
    int pre[105]={-1};
    int nxt[105]={-1};
    int main()
    {
        int n;
        cin>>n;
        char s[105];
        scanf("%s",s);
        vector<char>v;
        int i,j,k,ans=0;
        for(i=0;i<n;i++)v.push_back(s[i]);
        for(i=25;i>=0;i--)//从z开始判断并删除 
        {
            vector<char>::iterator it = v.begin();
            while(it!=v.end())
            {    
                if(v.size()==1)break;
                if(it==v.begin())
                {
                    if(*it=='a'+i&&*(it+1)=='a'+i-1)
                    {
                        it=v.erase(it);
                        ans++;
                        it=v.begin();//删掉一个后回到开头重新判断 
                    }
                    else it++;
                }
                else if(it==v.end()-1)
                {
                    if(*it=='a'+i&&*(it-1)=='a'+i-1)
                    {
                        it=v.erase(it);
                        ans++;
                        it=v.begin();
                    }
                    else it++;
                }
                else
                {
                    if((*(it-1)=='a'+i-1||*(it+1)=='a'+i-1)&&*it=='a'+i)
                    {
                        it=v.erase(it);
                        ans++;
                        it=v.begin();
                    }
                    else it++;
                }
            }
    
        }
        cout<<ans;
        return 0;
    }


  • 相关阅读:
    Watchguard公司内部招聘:C Developer in Linux Requirements
    条件注释判断浏览器<![if !IE]><![if IE]><![if lt IE 6]><![if gte IE 6]>
    js之事件冒泡和事件捕获详细介绍
    javascript:;与javascript:void(0)使用介绍
    IE和FireFox中JS兼容之event .
    Adobe下周将推新补丁和新的更新模式 狼人:
    微软下周二发布11个补丁 修复25个安全漏洞 狼人:
    安全专家担心Adobe没有足够实力来阻止黑客攻击 狼人:
    保证安全 认清五种易被忽视的攻击方式 狼人:
    六成黑客攻击与PDF漏洞有关 远超微软 狼人:
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12399254.html
Copyright © 2011-2022 走看看