zoukankan      html  css  js  c++  java
  • HDU5455 沈阳网络赛 Fang Fang

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5455

    题意是

    Fang Fang says she wants to be remembered.
    I promise her. We define the sequence F of strings.
    F0 = f",
    F1 = ff",
    F2 = cff",
    Fn = Fn1 + f", for n > 2
    Write down a serenade as a lowercase string S in a circle, in a loop that never ends.
    Spell the serenade using the minimum number of strings in F, or nothing could be done but put her away in cold wilderness.

     
    Input
    An positive integer T, indicating there are T test cases.
    Following are T lines, each line contains an string S as introduced above.
    The total length of strings for all test cases would not be larger than 106.
     
    Output
    The output contains exactly T lines.
    For each test case, if one can not spell the serenade by using the strings in F, output 1. Otherwise, output the minimum number of strings in F to split S according to aforementioned rules. Repetitive strings should be counted repeatedly.
     
      题意大概就是给你一个首尾相接的cf串, 求出他最少有几个Fn串组成, 比如cffcfff最少由两个串组成, 1:cff 2:cfff, 观察答案我们感觉到这个题似乎与c有关, 因此对于每一个c我们求出其后面f的个数再加以判断,另外还需考虑没有c的情况, 此题还有一个坑点就是输入不保证全是由c f组成,
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    char str[1000000 + 100];
    
    int main() {
        int T;
        scanf("%d", &T);
        int kase = 0;
        while(T--) {
            scanf("%s", str);
            int len = strlen(str);
            bool qita = false;
    
            for(int i=0; i<len&&!qita; i++){
                if(str[i]!='c'&&str[i]!='f') qita=true;
            }
            if(qita) {
                printf("Case #%d: -1
    ", ++kase);
                continue;
            }
            int geshu = 0;
            for(int i=0; i<len&&str[i]!='c'; geshu++, i++);   //由于是首尾相接, 我们求出开头f的个数
    //        printf("geshu = %d
    ", geshu);
            bool flog = true;
            int res = 0;
            for(int i=len-1; i>=0; i--){
                if(str[i]=='c'){           
                    res++;
                    if(geshu < 2) {          //此时的geshu就是当前的c后面f的个数
                        flog = false;
                        break;
                    }
                    geshu = -1;
                }
                geshu++;
            }
    
            if(res == 0){                     //处理没有c的情况
                geshu /= 2;                   //没有c的时候我们求出的f个数是实际个数的两倍
                if(geshu%2==0) res = geshu/2; 
                else res = geshu/2+1;
            }
            if(flog) {
                printf("Case #%d: %d
    ", ++kase, res);
            }else{
                printf("Case #%d: -1
    ", ++kase);
            }
        }
        return 0;
    }
     
     
  • 相关阅读:
    CentOS7使用集群同步脚本对文件同步分发
    CentOS7安装jdk1.8
    CentOS7+CDH5.12.1集群搭建
    nginx通配符
    Nginx反向代理及配置
    一些好玩有用的书签
    linux操作小技巧锦集
    系统压测结果对比:tomcat/thinkphp/swoole/php-fpm/apache
    python修改linux日志(logtamper.py)
    【原创】给定随机数的取值范围(最小值、最大值),且要求多次取得的随机数最后的结果有一个固定的平均值
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5337507.html
Copyright © 2011-2022 走看看