hdu 5455 Fang Fang(2015 ACM/ICPC Asia Regional Shenyang Online)

来源:互联网 发布:淘宝店铺装修模板下载 编辑:程序博客网 时间:2024/06/16 18:54

点击打开链接

Fang Fang

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


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 = 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 Saccording to aforementioned rules. Repetitive strings should be counted repeatedly.
 

Sample Input
8ffcfffcffcffcffcfffcffcffcffcfffffcffcfffcffcfffcffffcfffffcffcffc
 

Sample Output
Case #1: 3Case #2: 2Case #3: 2Case #4: -1Case #5: 2Case #6: 4Case #7: 1Case #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".
 

Source
2015 ACM/ICPC Asia Regional Shenyang Online

这个题还是有点坑的,有多种情况,有可能出现非c 和 f 字符,也有可能出现

空行,,艾玛, 这题都快把我坑苦了,交流好几次都不对!!!

其实只要注意了这些东西也就没有问题了

解题思路:

首先将不是c 和 f 的字符扣去,输出-1;

先判断一下两个c之间的 f 数目,然后再判断开头和结尾就ok了


上代码吧:

/**2015 - 09 - 20 下午Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <set>using namespace std;typedef long long LL;const int maxn = 1e6+5;const double eps = 1e-7;char str[maxn];int Init(){    int len, sum=0, flag=0;    int x1, x2;    len = strlen(str);    for(int i=0; i<len; i++)    {        if(str[i] == 'c')        {            flag = 1;            x1 = i;            x2 = i;            sum = 1;            break;        }    }    if(!flag)        return (len+1)/2;    for(int i=x1+1; i<len; i++)    {        if(str[i] == 'c')        {            if(i-x1 < 3)                return -1;            x1 = i;            sum++;        }    }    if(x2+len-x1-1 < 2)        return -1;    return sum;}int main(){    int T,cas;    cin>>T;    getchar();    for(cas=1; cas<=T; cas++)    {        gets(str);        if(str[0] == '\n')            printf("Case #%d: %d\n",cas, 0);        else        {            bool ok = false;            int len = strlen(str);            for(int i=0; i<len; i++)            {                if(str[i]!='c' && str[i]!='f')                {                    ok = true;                    break;                }            }            if(ok)                printf("Case #%d: %d\n",cas, -1);            else                printf("Case #%d: %d\n",cas, Init());        }    }    return 0;}



0 0
原创粉丝点击