zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    linux Mint 安装apache2
    linux Mint 安装tomcat8
    linux Mint wine安装qq,桌面快捷键配置
    linux Mint mysql 安装
    卸载linux Mint自带jdk并安装最新jdk
    linux Mint截图软件 Shutter
    linux git安装及配置(包括更新)
    linux安装wine
    百度地图用ip获取当前位置的经纬度(高精度)
    mysql 索引和视图
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5773678.html
Copyright © 2011-2022 走看看