(字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5455

Fang Fang

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 233    Accepted Submission(s): 110

Problem Description

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 = Fn−1 + ‘‘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.

Sample Input

8

ffcfffcffcff

cffcfff

cffcff

cffcf

ffffcffcfff

cffcfffcffffcfffff

cff

cffc

Sample Output

Case #1: 3

Case #2: 2

Case #3: 2

Case #4: -1

Case #5: 2

Case #6: 4

Case #7: 1

Case #8: -1

Hint

Shift the string in the first test case, we will get the string "cffffcfffcff"
and it can be split into "cffff", "cfff" and "cff".

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

#define N 1100000
char s[N];

int main()
{
    int t, iCase=1;
    scanf("%d", &t);

    while(t--)
    {
        int i, num=0, sum=0, flag=0, len;

        scanf("%s", s);

        len = strlen(s)-1;

        for(i=0; i<=len; i++)
        {
            if(s[i]==‘f‘)
                num++;
            if(s[i]!=‘f‘ && s[i]!=‘c‘)
                flag = 1;
            if(s[i]==‘c‘)
            {
                if(i==len-1 && s[0]==‘f‘ && s[len]==‘f‘)
                    sum ++;
                else if(i==len && s[0]==‘f‘ && s[1]==‘f‘)
                    sum ++;
                else if(s[i+1]==‘f‘ && s[i+2]==‘f‘)
                    sum++;
                else
                    flag = 1;
            }
        }

        printf("Case #%d: ", iCase++);

        if(flag)
            printf("-1\n");
        else
        {
            if(sum)
                printf("%d\n", sum);
            else if(len+1==num)
                printf("%d\n", (num+1)/2);
            else
                printf("-1\n");
        }
    }
    return 0;
}
时间: 2024-12-03 09:12:28

(字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)的相关文章

【题解】 2015 ACM/ICPC Asia Regional Shenyang Online

[1006] FangFang (暴力枚举) Fang Fang Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 871    Accepted Submission(s): 364 Problem Description Fang Fang says she wants to be remembered. I promise her.

2015 ACM/ICPC Asia Regional Shenyang Online

1001 Traversal 1002 Best Solver 1003 Minimum Cut 1004 Dividing This Product 1005 Excited Database 1006 Fang Fang 1007 Matches Puzzle Game 1008 Hold Your Hand 1009 Stability 1010 Jesus Is Here 1011 Poker 1012 Largest Point 1013 Manors

Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩溃的.(吐血......) 可是仔细观察,它指数大,可是mod小啊,它吓人,可是可以暴力搞啊!! 这个题目一个难点就是要向下取整求余,详解见传送门,本题是向下取整,也就是向上取整加一. 还有就是指数太大,要找到循环节,其实由于mod小,循环节并没有太大,暴力跑就ok啦!  此刻内心是崩溃的 1 #i

Hdu 5452 Minimum Cut (2015 ACM/ICPC Asia Regional Shenyang Online) dfs + LCA

题目链接: Hdu 5452 Minimum Cut 题目描述: 有一棵生成树,有n个点,给出m-n+1条边,截断一条生成树上的边后,再截断至少多少条边才能使图不连通, 问截断总边数? 解题思路: 因为只能在生成树上截断一条边(u, v),所以只需要统计以v为根节点的子生成树里的节点与子生成树外的节点的边数就可以了.对于新加入的边(u', v')来说,只影响以LCA(u, v)为根节点的子树里面的节点.统计所有答案,扫一遍输出最小即可.(比赛的时候只统计叶子节点,给水过去了........233

Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推

题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我们,sn肯定由sn-1与sn-2推导出来的.然后呢,我们可以看出 n%2==1 的时候 sn-1 与 sn-2 由 ffff 衔接起来的,n%2==0 的时候,sn-1 与 sn-2由 ff 衔接起来的.告诉队友后,队友就把这个当成重要依据推啊,推啊!!到最后感觉丢队友自己看药丸,放弃02回来和队友

HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

题目大意:给一个N个点M条边的无向图,有Q个询问:1.删掉a.b之间所存在的边:2.询问有多少条边,单独删掉之后a与b不再连通. 思路:脑洞大开. 对于询问,首先想到的就是a与b之间有多少桥(割边),然后想到双连通分量,然而删边是个坑爹的问题,于是我们离线倒着来,把删边变成加边. 双连通分量这种东西呢,其实缩点连起来之后,就是一棵树辣. 然后询问两个点的时候,设根到点x的距离为dep[x],a.b的最近公共祖先为lca(a, b),那么询问query(a, b) = dep[a] + dep[b

HDU 6198(2017 ACM/ICPC Asia Regional Shenyang Online)

思路:找规律发现这个数是斐波那契第2*k+3项-1,数据较大矩阵快速幂搞定. 快速幂入门第一题QAQ #include <stdio.h> #include <stdlib.h> #include <cmath> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <vector> #inc

(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2061    Accepted Submission(s): 711 Problem Description Jack likes to travel around the wo

2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 329 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very