Fang Fang HDU - 5455 (思维题)

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 }

原文地址:https://www.cnblogs.com/Cherry93/p/10029924.html

时间: 2024-07-28 20:43:51

Fang Fang HDU - 5455 (思维题)的相关文章

hdu 4883 思维题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 566    Accepted Submission(s): 267 Problem Description TIANKENG manages a

hdu 4810 思维题+二进制位规律+异或规律 213南京现场赛题

http://acm.hdu.edu.cn/showproblem.php?pid=4810 以前做过一些涉及异或的题,化为二进制形式,然后统计0,1个数是一种很常见的处理方法,但是在做这个题的时候居然没尝试,脑残啊...... 一开始看5s时限,感觉稍微暴力一点应该可以,于是YY的O(n^3)算法但是没去实现,明显超时啊,大致就是通过C(n,1)的组合可以在O(n^2)内处理出C(n,2)的组合,在通过C(n,2)处理出C(n,3)的组合....但是C(n,2)已经是n^2个数了,所以算法是O

hdu 5014 思维题/推理

http://acm.hdu.edu.cn/showproblem.php?pid=5014 从小数开始模拟找方法规律,然后推广,尤其敢猜敢尝试,错了一种思路继续猜-----这是一种很重要的方法啊 这道题还是从小数开始模拟,我是根据16以内的找的规律 根据 2^k---2^k-1 2^k+1---2^k-2 ... 这样陪下去 当2^k==n的时候, 从2^(k-1)按同样的方法配下去, WA了很久,是lower_bound用错了 不过没明白为啥  换成upper_bound也可以AC....但

Just Random HDU - 4790 思维题(打表找规律)分段求解

Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In each game the following will be done:  1. Coach Pang randomly choose a integer x in [a, b] with equal probability.  2. Uncle Yang randomly choose a i

朋友HDU - 5963 (思维题) 三种方法

传送门 题目描述 输入 输出 样例输入 2 2 3 1 2 0 0 1 1 2 1 1 0 2 4 11 1 2 1 2 3 1 3 4 0 0 1 0 2 0 3 0 4 1 2 1 0 0 1 0 2 0 3 1 3 4 1 0 3 0 4 Sample Input 样例输出 Boys win! Girls win! Girls win! Boys win! Girls win! Boys win! Boys win! Girls win! Girls win! Boys win! Girl

hdu 5491 思维题

#include<stdio.h> #include <math.h> #include <algorithm> #include <math.h> #include <string.h> #include <bitset> #include <iostream> using namespace std; #define LL long long void out(LL x) { bitset<32>s(x);

hdu 5455 Fang Fang 坑题

Fang Fang Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5455 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",F

(字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5455 Fang Fang Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 233    Accepted Submission(s): 110 Problem Description Fang Fang says she wants to be

HDU - 5455 Fang Fang

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 > 2Write down a serenade as a lowercase string S in a circle,