hdu5455Fang Fang 水题

//给出一个字符串str
//f1 = 'f'
//f2 = 'ff'
//f3 = 'cff'
//fn = fn-1 + 'f'
//问最少需要将这个字符串分为由fn组成的字符串
//直接扫一遍就行 , 只是要注意空字符串和字符串
//中有不是'f'和'c' 的情况
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string"
#include"string.h"
#include"cmath"
#include"queue"
#include"stack"
#include"map"
#include"vector"
#include"ctype.h"
using namespace std;
const int maxn = 1e6+10 ;
char str[maxn] ;
int main()
{
    int t ;
    int cas = 0 ;
    scanf("%d" ,&t) ;
    getchar() ;
    while(t--)
    {
        gets(str) ;
        int st = -1 ;
        int s = 0 ;
        int n = strlen(str);
        bool flag = false ;
        for(int i = 0 ;i < n;i++)
        if(str[i] == 'c')
        {
            if(st == -1)
            st = i ;
            s++ ;
        }
        else if(str[i] != 'f')
            flag = true ;
        printf("Case #%d: " , ++cas) ;
        if(n == 0)
        {
            puts("0") ;
             continue ;
        }
        if(flag)
        {
            puts("-1");
            continue ;
        }
        if(s== 0)
        {
            printf("%d\n" , (n+1)/2);
            continue ;
        }

        int i = st ;
        int sum = 0 ;
        while(1)
        {
             i = (i + n + 1)%n ;
            if(str[i] == 'c')
            {
                if(sum < 2)
                {
                    flag = true ;
                    break ;
                }
                else sum = 0 ;
            }
            else sum++ ;
           if(i == st)
                break ;
        }
        if(flag)
        puts("-1");
        else cout<<s<<endl;
    }
    return 0;
}


你可能感兴趣的:(hdu5455Fang Fang 水题)