1. 程式人生 > >Fang Fang HDU - 5455 (思維題)

Fang Fang HDU - 5455 (思維題)

var mathjax mini style test sta 個數 name ane

Fang Fang says she wants to be remembered.
I promise her. We define the sequence FF of strings.
F0 = f",F0 = ‘‘f",
F1 = ff",F1 = ‘‘ff",
F2 = cff",F2 = ‘‘cff",
Fn = Fn?1 + f", for n > 2Fn = Fn?1 + ‘‘f", for n > 2
Write down a serenade as a lowercase string SS in a circle, in a loop that never ends.
Spell the serenade using the minimum number of strings in
FF, or nothing could be done but put her away in cold wilderness.

InputAn positive integer TT, indicating there are TT test cases.
Following are TT lines, each line contains an string SS as introduced above.
The total length of strings for all test cases would not be larger than 106106.
OutputThe output contains exactly

TT lines.
For each test case, if one can not spell the serenade by using the strings in FF, output ?1?1. Otherwise, output the minimum number of strings in FF to split SSaccording 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".

題意很簡單不說了。
題解:sumf記錄f的個數,然後遇見一個c判斷一下是不是符合題意,如果‘c’在[0,len-1)區間內,判斷一下它的下一個以及下下個是不是f,因為這個字符串是循環串,所以如果這個c是倒數第二個,判斷一下最後一個和第一個字符是不是f
如果最後一個字符是c,判斷一下第一個和第二個字符是不是f,如果是,sumc++;

不存在的時候就是sumf+sumc!=字符串的長度就行了

 代碼如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<stack>
 6 #include<queue>
 7 #include<map>
 8 #include<algorithm>
 9 using namespace std;
10 typedef long long ll;
11 int main()
12 {
13     int T,t=1;
14     scanf("%d",&T);
15     while(T--)
16     {
17          string a;
18          cin>>a;
19 
20          int len=a.length();
21          int sumc=0,sumf=0,flag=0;
22          for(int i = 0;i < len;i++)
23          {
24              if(a[i]==f)
25                 sumf++;
26              else if(a[i]==c)
27              {
28                  if(a[i+1]==f&&a[i+2]==f)
29                     sumc++;
30                  if(i==len-2)
31                  {
32                      if(a[i+1]==f&&a[0]==f)
33                         sumc++;
34                  }
35                  if(i==len-1)
36                  {
37                      if(a[0]==f&&a[1]==f)
38                         sumc++;
39                  }
40              }
41          }
42          printf("Case #%d: ",t++);
43          if(sumc+sumf!=len)
44              printf("-1\n");
45          else
46          {
47              if(sumc==0) 
48                     printf("%d\n",sumf+1>>1); 
49              else
50                 printf("%d\n",sumc);
51          } 
52     } 
53     return 0;
54 }

Fang Fang HDU - 5455 (思維題)