Codeforces Round #188 (Div. 2) B题

这题意思很清楚,不过我们也可以通过样例知道!思路是:只要出现metal,然后前面出现几个heavy就可以组合成几种!

这里有两种答案:

一:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000000+10;
char a[3][6]= {"heavy","metal"};
char s[maxn];
int main()
{
    while(cin>>s)
    {
        __int64 f=0;
        __int64 sum=0;
        for(int i=0; s[i]!='\0'; i++)
        {
            if(strncmp(a[0],s+i,5)==0)//这里利用了strncmp函数!
            {
                f++;
            }
            else if(strncmp(a[1],s+i,5)==0)
            {
                sum+=f;
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

二:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[2][6]={"heavy","metal"};
const int maxn=1000005;
char s[maxn];
int main()
{
    int i;
    while(cin>>s)
    {
        __int64 f=0;
        __int64 sum=0;
        for(i=0; s[i]!='\0'; i++)
        {
            if(s[i]==a[0][0]&&s[i+1]==a[0][1]&&s[i+2]==a[0][2]&&s[i+3]==a[0][3]&&s[i+4]==a[0][4])
            {
                f++;
                i+=4;
            }
            else if(s[i]==a[1][0]&&s[i+1]==a[1][1]&&s[i+2]==a[1][2]&&s[i+3]==a[1][3]&&s[i+4]==a[1][4])
            {
                sum+=f;
                i+=4;
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值