Fang Fang
Time Limit : 1500/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 0
Problem Description
Fang Fang says she wants to be remembered.
I promise her. We define the sequence $F$ of strings.
$F_{0}\ =\ ``\texttt{f}",$
$F_{1}\ =\ ``\texttt{ff}",$
$F_{2}\ =\ ``\texttt{cff}",$
$F_{n}\ =\ F_{n-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 $10^6$.
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".
[/hint]
//题意:给定字符串,可以移动字符,但只能是从开头移到结尾,不能将其随意插入。
//问移动后有几个cff...字符串。
//但若出现情况1、2时输出-1.
// 1、字符串中有除c、f以外的字符
// 2、字符串中cff...个数小于2.
//还有,若字符串全为f,若f的个数num为奇数,输出(num/2)+1,否则输出num/2
//唉,技术就是不行啊!!!写了一晚上还是错的,明天继续。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2*1000010];
int b[1000100];
int main()
{
int t,c1,f1,z,i,k,l1;
int cot;
while(scanf("%d",&t)!=EOF)
{
int cnt=1;
k=0;
while(t--)
{
memset(b,0,sizeof(b));
c1=f1=z=l1=k=0;
cot=0;
scanf("%s",a);
int l=strlen(a);
for(i=0;i<l;i++)
{
if(a[i]=='c')
c1++;
else if(a[i]=='f')
f1++;
else
z++;
}
printf("Case #%d: ",cnt++);
if(z>0)
printf("-1\n");
else if(f1==l)
{
if(f1&1)
printf("%d\n",l/2+1);
else
printf("%d\n",l/2);
}
else
{
for(i=0;i<l+l1;)
{
if(a[i]!='c')
{
a[l+l1]=a[i];
l1++;
i++;
}
else
break;
}
for(i=l1;i<l+l1;)
{
i++;
if(a[i]!='c')
{
b[k]++;
i++;
}
else
{
if(b[k]>=2)
{
k++;
i++;
cot++;
}
else
{
printf("-1\n");
break;
}
}
}
printf("%d\n",cot);
}
}
}
return 0;
}
//唉,终于对了,重新计数这块不行啊,还是不会灵活运用。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2*1000010];
int main()
{
int t,c,f,i,j,l1;
int cnt=1;
scanf("%d",&t);
while(t--)
{
scanf("%s",a);
printf("Case #%d: ",cnt++);
int l=strlen(a);
c=f=j=0;
for(i=0;i<l;i++)
{
if(a[i]=='c')
c++;
else if(a[i]=='f')
f++;
else
j++;
}
if(j!=0)
printf("-1\n");
else if(f==l)
{
if(f&1)
printf("%d\n",f/2+1);
else
printf("%d\n",l/2);
}
else
{
for(i=0,l1=0;i<l+l1;)
{
if(a[i]!='c')
{
a[l+l1]=a[i];
l1++;
i++;
}
else
break;
}
//for(i=l1;i<l+l1;i++)
// printf("%c",a[i]);
// printf("\n");
int k=0,flag=1;
for(i=l1;i<l+l1;)
{
if(!flag)
break;
if(a[i]=='c')
{
int x=0;
i++;
while(a[i]=='f'&&i<l+l1)
{
x++;
i++;
}
if(x<2)
{
flag=0;
break;
}
k++;
}
}
if(flag)
printf("%d\n",k);
else
printf("-1\n");
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2*1000010];
int main()
{
int t,c,f,i,j,l1;
int cnt=1;
scanf("%d",&t);
while(t--)
{
scanf("%s",a);
printf("Case #%d: ",cnt++);
int l=strlen(a);
c=f=j=0;
for(i=0;i<l;i++)
{
if(a[i]=='c')
c++;
else if(a[i]=='f')
f++;
else
j++;
}
if(j!=0)
printf("-1\n");
else if(f==l)
{
if(f&1)
printf("%d\n",f/2+1);
else
printf("%d\n",l/2);
}
else
{
for(i=0,l1=0;i<l+l1;)
{
if(a[i]!='c')
{
a[l+l1]=a[i];
l1++;
i++;
}
else
break;
}
//for(i=l1;i<l+l1;i++)
// printf("%c",a[i]);
// printf("\n");
int k=0,flag=1;
for(i=l1;i<l+l1;)
{
if(!flag)
break;
if(a[i]=='c')
{
int x=0;
i++;
while(a[i]=='f'&&i<l+l1)
{
x++;
i++;
}
if(x<2)
{
flag=0;
break;
}
k++;
}
}
if(flag)
printf("%d\n",k);
else
printf("-1\n");
}
}
return 0;
}