您的位置:首页 > 其它

hdu 5455 Fang Fang

2015-09-19 18:53 477 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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 Fto split S according to aforementioned rules. Repetitive strings should be counted repeatedly.

[align=left]Sample Input[/align]

8
ffcfffcffcff

cffcfff
cffcff

cffcf

ffffcffcfff

cffcfffcffffcfffff
cff

cffc

[align=left]Sample Output[/align]

Case #1:3

Case #2: 2

Case #3: 2

Case #4: -1

Case #5: 2

Case #6: 4
Case #7: 1

Case #8: -1

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[100006];
int main()
{
int t,i,ans,j,flag,k=1,a;
scanf("%d",&t);
getchar();
while (t--)
{
flag=0;
gets(s);
if (s[0]==' ')
{
printf("Case #%d: ",k);
k++;
printf("0\n");
continue;
}
for (i=0;s[i];i++){
if (s[i]!='c'&&s[i]!='f') {flag=1;break;}
if (s[i]=='c') break;
}
if (s[i]=='\0')
{
printf("Case #%d: ",k);
k++;
printf("%d\n",i/2+i%2);
continue;
}
a=i;
ans=1;
j=0;
for (i=i+1;s[i];i++)
{
if (s[i]!='c'&&s[i]!='f') {flag=1;break;}
if (s[i]!='c') j++;
else
{
if (j>=2)
{
j=0;
ans++;
}
else
{
flag=1;
break;
}
}
}
if (j+a<2) flag=1;
printf("Case #%d: ",k);
if (flag) printf("-1\n");
else printf("%d\n",ans);
k++;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: