码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5455 沈阳网络赛 Fang Fang

时间:2016-03-30 16:25:32      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

题目地址: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\n", ++kase);
            continue;
        }
        int geshu = 0;
        for(int i=0; i<len&&str[i]!=c; geshu++, i++);   //由于是首尾相接, 我们求出开头f的个数
//        printf("geshu = %d\n", 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\n", ++kase, res);
        }else{
            printf("Case #%d: -1\n", ++kase);
        }
    }
    return 0;
}

 

 
 

HDU5455 沈阳网络赛 Fang Fang

标签:

原文地址:http://www.cnblogs.com/xingxing1024/p/5337507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!