【C题—NEUQ】第九届“图灵杯”NEUQ-ACM程序设计竞赛个人赛

题目:NEUQ
在这里插入图片描述
样例:
在这里插入图片描述

AC代码考场版:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

char sc[] = {'0','0','0','0','0'};
int sum, cnt;

int main(void)
{
    int n;
    cin>> n;
    sum = n;
    char c;
    for(int i = 1; i <= n; i++)
    {
        cin>> c;
        if(sc[1] == '0' && c == 'N')
            sc[1] = 'N';
        if(sc[1] == 'N' && sc[2] == '0' && c == 'E')
            sc[2] = 'E';
        if(sc[1] == 'N' && sc[2] == 'E' && sc[3] == '0' && c == 'U')
            sc[3] = 'U';
        if(sc[1] == 'N' && sc[2] == 'E' && sc[3] == 'U' && sc[4] == '0' && c == 'Q')
        {
            sc[4] = 'Q';
            for(int j = 1; j <= 4; j++)
                sc[j] = '0';
            cnt += 4;
        }
    }
    cout<< sum - cnt << endl;  
    return 0;
}

从大佬的代码中找了几个。优雅,太优雅了,贴在这了。

AC代码优化版(更加清晰了些):

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string str;
    cin>>str;
    string str2="NEUQ";
    int cnt=0,ans=0;
    for(int i=0;i<n;i++)
    {
        if(str[i]==str2[cnt]) cnt++;
        else ans++;
        if(cnt==4) cnt=0;
    }
    if(cnt!=0) ans+=cnt;
    cout<<ans;
    return 0;
}

双重循环。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> 
using namespace std;

const int N = 1e6 + 10;
int n;
char str[N];
char s[] = {'N', 'E', 'U', 'Q'};

int main() 
{
    scanf("%d%s", &n, str);
    int cnt = 0, j = 0;
    for(int i = 0; i < n; i ++) 
    {
        if(str[i] == s[j]) 
        {
            j ++;
            if(j == 4) cnt ++, j = 0;
        }
    }
    printf("%d\n", n - 4 * cnt);
    return 0;
}

AC代码(特别注意注释的循环)

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int n;
    cin>>n;
    string s;
    string t = "NEUQ";
    cin>>s;
     
    int idx = 0,count = 0;
    for(int i = 0; s[i]; i++) //字符串最后是\0
    {
        if(s[i]==t[idx])
        {
            idx++;
            if(idx==4)
            {
                idx = 0;
                count++;
            }
        }
    }
    cout<<n-4*count;
    return 0;
}

欢迎交流~ 祝大家天天快乐AC~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qing小星星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值