题解 P1684 【考验】

· · 题解

本题可以用贪心来做(本人动规不咋地QAQ)

思路:

首先按顺序用map来记录当前每个韵脚出现次数

题目中要求四行诗满足“AABB”、“ABAB”、“ABBA”和“AAAA”中的一种,即有一种韵脚出现四次或两种韵脚各出现两次

故若前出现次数超过两次的韵脚数有两个或有一个韵脚出现次数出现次数为四次,答案就加一且清空记录的数

代码如下:

# include <map>
# include <cstdio>
using namespace std;
int n,x;
map <int,int> f;
int s;
int ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        f[x]++;
        if(f[x]==2) s++;
        if(s==2)
        {
            ans++;
            s=0;
            f.clear();
        }
        if(f[x]==4)
        {
            ans++;
            s=0;
            f.clear();
        }
    }
    printf("%d",ans);
    return 0;
}