Fang Fang hdu 5455

http://acm.hdu.edu.cn/showproblem.php?pid=5455

题意:判断字符串最少符合题意的个数,它是一个环。若c前面有f,则把f的个数都加到后面去。还有一个坑点是,会有其他字母,不止有c,f。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 110000;
char str[maxn];
typedef long long LL;

int main()
{
   int T, cnt=1;

   scanf("%d", &T);

   while(T --)
   {
       scanf("%s", str);

       int flag = 0;
       ///判断有没有其他字母
       for(int i=0; str[i]; i++)
      if(str[i]!='c' && str[i]!='f')
       {
           flag = 1;
           break;
       }

       if(flag)
       {
          printf("Case #%d: -1
", cnt++);
           continue;
       }

       int k=0;
       
       ///判断是否全部为f
       while(str[k]=='f') k++;
       int len = strlen(str);
       ///若全部为f则输出(k+1)/2,因为要求最少的个数,所以两个f在一块才会最少,一个f也符合情况
        if(k==len&&str[k]!='c')
        {
            printf("Case #%d: %d
", cnt++, (k+1)/2);
            continue;
        }

       int ans = 0;
       int f = 0;
       
       ///已经判断过是否只有c和f了,所以k+1一定为f
       for(int i=k+1; str[i]; i++)
       {
           if(str[i]=='c')
           {
              if(f>1) ans ++;///若符合题意,则ans+1:
              else///若不符合题意,则直接跳出循环,输出“-1”
              {
                  flag =1;
                  break;
              }
               f = 0;
           }
           else if(str[i]=='f')
            f ++;
       }

       if(f+k>1) ans++;///判断最后一个c后面的f的个数是否符合题意
       else flag = 1;

       if(flag) printf("Case #%d: -1
", cnt++);
       else printf("Case #%d: %d
",cnt++, ans);
   }
    return 0;
}
/*
99
fffff
fff
ccff
fds
cfcf
fffcffcf
*/
View Code
原文地址:https://www.cnblogs.com/daydayupacm/p/5773678.html