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;
    }
     
     
  • 相关阅读:
    《原創》實現禁止 WTL CTabView 中標籤的拖曳行為。 (Disable Dragging Operation of CTabView)
    《轉貼》ATL NTService 運作流程
    《轉貼》WTL 之 m_hWndMDIClient
    《原創》加強版的 C++ 字串型別
    《原創》建立最基礎的 Irrlicht 3D 引擎的應用框架。(for vc2008)
    《轉貼》關於 ios::app 與 ios::ate 簡易說明
    《轉貼》WTL for MFC programmer 系列文章
    《轉貼》以Virtual Inheritance及Template讓C++能實現C#中的sealed
    Oracle学习笔记(concat和substr)
    Oralce学习笔记(sql取一张表的数据插入另一张表)
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5337507.html
Copyright © 2011-2022 走看看