• 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;
    }


  • 相关阅读:
    很酷的软件WinDirStat
    [致歉]电信机房网络问题造成无法访问博客园
    [新功能]团队Blog群发邮件
    在SharePoint上建立新闻应用
    Firefox中Javascript使用event对象需要注意的问题
    祝大家春节快乐
    SPS用户管理的问题
    对AreaCollection中的元素进行排序—SharePoint新闻列表WebPart开发手记
    [小改进]给链接增加了描述属性
    Outlook 2003通过ISA 2004无法访问外部邮件(SMTP/POP3)的处理方法
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12399254.html
Copyright © 2020-2023  润新知