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

来源:互联网 发布:淘宝网卡包 编辑:程序博客网 时间:2024/05/31 19:44

因为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 5455const 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