您的位置:首页 > 其它

Fang Fang---hud5455(字符串处理)

2015-09-20 10:58 260 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5455

就是求字符串中含有几个f[i], 输出最小的;

例如fff应该是2,有f[0]和f[1]组成的;

ffcffc也是2是有个cff组成的,因为字符串是一个环;

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

char s
;

int main()
{
int T, t=1, cnt, len, flag, ans;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
len = strlen(s);
cnt=flag=ans=0;
for(int i=0; i<len; i++)
{
if(s[i]!='c'&&s[i]!='f')///出现其他字符;
{
flag = 1;
break;
}
if(s[i] == 'f')///以便判断是否全是f;
cnt++;
if(s[i] == 'c')
{
if(i<=len-3&&s[i+1]=='f'&&s[i+2]=='f')
ans++;
else if(i==len-2&&s[i+1]=='f'&&s[0]=='f')
ans++;
else if(i==len-1&&s[0]=='f'&&s[1]=='f')
ans++;

else
{
flag=1;
break;
}
}
}
if(cnt==len)
{
printf("Case #%d: %d\n", t++, (len+1)/2);
}
else if(flag==1)
{
printf("Case #%d: -1\n", t++);
}
else
printf("Case #%d: %d\n", t++, ans);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: