[Codecforces]1011 A. Stages

題目URL : http://codeforces.com/contest/1011/problem/A

啊啊啊啊啊~~來刷水題囉~~(練練手速&提升自信的好管道)
單純標記+由小掃到大取不可相臨的字母。


#include<bits/stdc++.h>
using namespace std;
int cho[30];
char str[1005];
main()
{
    int a,b,ans=0;
    cin >> a >> b;
    for(int i=0;i<a;i++)
    {
        cin >> str[i];
        cho[str[i]-96]++;
    }
    int tot=b;
    for(int i=1;i<=26;i++)
    {
        if(tot==0)
        break;
        else if(tot!=0 && cho[i]>=1)
        {
            if(cho[i-1]!=-1)
            {
                ans+=i;
                tot--;
                cho[i]=-1;
            }
        }
    }
    if(tot==0)
        cout << ans << endl;
    else if(tot>0)
        cout << -1 << endl;
    return 0;
}

留言

這個網誌中的熱門文章

[ZJ] b229: TOI 2009 第一題:路徑問題

交大資工(APCS組)(面試&心得)

滿是挫傷的ION CAMP