hdu 5455 Fang Fang 2015 ACM/ICPC Asia Regional Shenyang Online

因为string S是一个loop,所以先要找的一个起始位置。显然将cff..f划分在一起是最优的(e.g. cff..f || cf..f || cf..f)。需要特判全都是f,exclude cf。另外,输入数据S不一定只由c和f构成,之前因为木有判断这一点WA了一次。

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>

using namespace std;
//hdu 5455
const int maxn=1e6+10;
char seq[maxn];
int T;
int ans;
void cal()
{
    int cntc=0;
    int idxc[maxn];
    memset(idxc,0,sizeof(idxc));
    int len=strlen(seq);
    for(int i=0;i<len;i++)
    {
        if(seq[i]=='c')
        {
            idxc[cntc++]=i;
        }
        if(seq[i]!='c'&&seq[i]!='f')//lead to WA without this, seq[] might not consist of only c and f
        {
            ans=-1;
            return;
        }
    }
    if(cntc==0)
    {
        if(len%2==0)
        {
            ans=len/2;
        }
        else
        {
            ans=len/2+1;
        }
        return;
    }
    int frontf=idxc[0];
    for(int i=0;i<cntc-1;i++)
    {
        int f=idxc[i+1]-idxc[i]-1;
        if(f<2)
        {
            ans=-1;
            return;
        }
        else
        {
            ans++;
        }
    }
    int f=frontf+len-idxc[cntc-1]-1;
    if(f<2)
    {
        ans=-1;
        return;
    }
    else
    {
        ans++;
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        scanf("%s",&seq);
        ans=0;
        cal();
        printf("Case #%d: %d\n",ca,ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值