Fang Fang
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 849 Accepted Submission(s): 350
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 S according 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 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: -1HintShift 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>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
const int inf=(1<<30);
char s[2000000];
int main()
{
int t,cnt1=1;
scanf("%d\n",&t);
while(t--)
{
scanf("%s",s);
int len=strlen(s);
int t=len;
int b=len;
int cnt,mark;
cnt=mark=0;
for(int i=0;i<len;i++)
{
if(s[i]=='f')
s[b++]='f';
if(s[i]=='c')
break;
}
s[b]='\0';
int len1=strlen(s);
printf("Case #%d: ",cnt1++);
if(t*2==b)
{
if(len%2==0)
printf("%d\n",len/2);
else
printf("%d\n",len/2+1);
continue;
}
for(int i=0;i<len;i++)
{
if(s[i]=='c')
cnt++;
if(s[i]!='f'&&s[i]!='c')
mark=1;
if(s[i]=='c'&&s[i+1]!='f')
mark=1;
if(s[i]=='c'&&s[i+2]!='f')
mark=1;
if(s[len-1]=='c'&&s[0]!='f')
mark=1;
if(s[len-1]=='c'&&s[1]!='f')
mark=1;
}
if(mark)
printf("-1\n");
else
printf("%d\n",cnt);
}
return 0;
}