题解 P4439 【[COCI2017-2018#3] Aron】

· · 题解

暴力出奇迹

刚看到这道题,不仅会心一笑:桶排模板题嘛

于是乎,三分钟搞定

#include<bits/stdc++.h>
using namespace std;
int t[100];
int main() {
    int n;
    long long ans=0;
    char c;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>c;
        t[c]++;
    }
    for(int i='A'; i<='Z'; i++) {
        if(t[i]>0) ans++;
    }
    cout<<ans<<endl;
    return 0;
}

结果自信一交(注意,没有编译) 映山红啊!!!!

然后再看题发现还要算自己(伤不起呀么伤不起)

于是就有了

cout<<ans+1<<endl;

我交

多了两个测试点。

再看题,发现

并且我们可以认为站在一个人后面的朋友都穿着匹配颜色的衬衫,而两个相邻的群体、相邻的个人或相邻的个人和一个集体将永远不会穿着相同颜色的衬衫。

额滴妈,太可怕了,这连筒排都不是 于是乎

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    long long ans;
    char c,last=' ';
    cin>>n;
    cin>>c;
    ans=0;
    for(int i=2; i<=n;) {
        last=c;
        while(c==last && i<=n) {
            cin>>c;
            i++;
        }
        ans++;
        if(i>n) break;
    }
    cout<<ans+1<<endl;
    return 0;
}

再交

emmmm,我被一道红题难住了

最后,发现自己一直在小题大做

一个for可以搞定的东西。。。

最后,上代码


#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    long long ans;
    char c,last;
    cin>>n;
    cin>>last;
    ans=0;
    ans++;
    for(int i=2; i<=n; i++) {
        cin>>c;
        if(c!=last)
        {
            ans++;
            last=c;
        }
    }
    cout<<ans+1<<endl;
    return 0;
}