题目链接: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[N]; 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 ", t++, (len+1)/2); } else if(flag==1) { printf("Case #%d: -1 ", t++); } else printf("Case #%d: %d ", t++, ans); } return 0; }