Fang Fang
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 552 Accepted Submission(s): 240
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.
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.
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.
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
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: -1HintShift the string in the first test case, we will get the string "cffffcfffcff" and it can be split into "cffff", "cfff" and "cff".
这道题耗费了一天的时间,我是先找c第一次的位置,从该位置开始遍历这个字符串,因为自己处理不当,在这里就有一个小BUG,在c前面会出现其他字母,当时这宗情况给忽略了,面对水题,却没a了,只能说明自己太水了
代码还能优化,先判断除了 c f还有没有其他的元素,这样的话就很好写了。
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char s[1000000+5];
int a[1000000+5];
int main()
{
int n;
scanf("%d",&n);
getchar();
for(int i=1;i<=n;i++)
{
gets(s);
int len=strlen(s);
int c1=len,c2,num=0;
for(int j=0;j<len;j++)
{
if(s[j]=='c')
{
c1=j;
break;
}
}
if(c1==len)
{
int flag=0;
for(int j=0;j<len;j++)
{
if(s[j]!='f')
{
flag=1;
break;
}
}
if(flag) num=-1;
else if(len%2==0) num=len/2;
else num=len/2+1;
}
else
{
int x=0;
a[0]=1;
c2=c1;
for(int k=c1+1;k<len;k++)
if(s[k]=='c')
{
if(k-c2<=2)
{
num=-1;
break;
}
else
{
a[++x]=1;
c2=k;
num++;
}
}
else if(s[k]=='f')
a[++x]=0;
else
{
num=-1;
break;
}
if(num!=-1)
{
if(c1!=0)
{
for(int k=0;k<c1;k++)
{
if(s[k]=='f')
a[++x]=0;
else
{
num=-1;
break;
}
}
if(num!=-1)
{
c2=c2-c1;
if(x-c2<2) num=-1;
else
num++;
}
}
else
{
c2=c2-c1;
if(x-c2<2) num=-1;
else num++;
}
}
}
printf("Case #%d: %d\n",i,num);
}
return 0;
}
本文介绍了一个关于FangFang序列的编程挑战,需要利用特定的字符串序列拼接成给定的目标字符串,并尽可能减少使用的序列数量。文章还提供了解决方案及代码实现。

1085

被折叠的 条评论
为什么被折叠?



