Fang Fang HDU - 5455 (思维题)
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!=字符串的长度就行了
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
int T,t=;
scanf("%d",&T);
while(T--)
{
string a;
cin>>a; int len=a.length();
int sumc=,sumf=,flag=;
for(int i = ;i < len;i++)
{
if(a[i]=='f')
sumf++;
else if(a[i]=='c')
{
if(a[i+]=='f'&&a[i+]=='f')
sumc++;
if(i==len-)
{
if(a[i+]=='f'&&a[]=='f')
sumc++;
}
if(i==len-)
{
if(a[]=='f'&&a[]=='f')
sumc++;
}
}
}
printf("Case #%d: ",t++);
if(sumc+sumf!=len)
printf("-1\n");
else
{
if(sumc==)
printf("%d\n",sumf+>>);
else
printf("%d\n",sumc);
}
}
return ;
}
Fang Fang HDU - 5455 (思维题)的更多相关文章
- hdu 4883 思维题
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Ja ...
- hdu 5014 思维题/推理
http://acm.hdu.edu.cn/showproblem.php?pid=5014 从小数開始模拟找方法规律,然后推广,尤其敢猜敢尝试,错了一种思路继续猜-----这是一种非常重要的方法啊 ...
- Just Random HDU - 4790 思维题(打表找规律)分段求解
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...
- HDU 6464 权值线段树 && HDU 6468 思维题
免费送气球 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 朋友HDU - 5963 (思维题) 三种方法
传送门 题目描述 输入 输出 样例输入 Sample Input 样例输出 Boys win! Girls win! Girls win! Boys win! Girls win! Boys win! ...
- hdu 5455 Fang Fang 坑题
Fang Fang Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5455 ...
- HDU - 5455 Fang Fang
Problem Description Fang Fang says she wants to be remembered.I promise her. We define the sequence ...
- (字符串处理)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) ...
- HDU 5455:Fang Fang 查cff个数
Fang Fang Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
随机推荐
- 算法竞赛入门经典 习题 2-10 排列(permutation)
习题 2-10 用1,2,3.....,9组成3个三位数abc.def和ghi,每一个数字恰好使用一次,要求abc:def:ghi=1:2:3.输出全部解. #include <stdio.h& ...
- 源代码编译安装MySQL5.6.12具体过程
1 下载安装包download tar.gzwget http://download.csdn.net/detail/mchdba/75450372 安装cmake软件包yum install cm ...
- 測试赛D - The War(有控制范围的贪心)
D - The War Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit St ...
- Qt Quick综合实例之文件查看器
假设你基于Qt SDK 5.3.1来创建一个Qt Quick App项目,项目模板为你准备的main.qml文档的根元素是ApplicationWindow或Window.这次我们就以Applicat ...
- apt --fix-broken install
1 自动修复安装出现broken的package 但是,如果还是失败的话,就需要手动进行干预了.
- 【C++程序不输出】到底是什么造成了程序不输出
(ubuntu 16.04) 最近做题的时候,经常莫名其妙地,程序写的明明没毛病但是就是输出不了,气得我呀 然后某一次突然发现了原因,竟然是输出之后没有加endl或者空格! 例如: cout<& ...
- C语言strdup()函数:复制字符串【转】
本文转载自:http://c.biancheng.net/cpp/html/166.html 头文件:#include <string.h> 定义函数:char * strdup(cons ...
- timeout in asp.net
Forms authentication timeout vs sessionState timeout They are different things. The Forms Authentica ...
- 【BZOJ 2457】 双端队列
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2457 [算法] 贪心 [代码] #include<bits/stdc++.h& ...
- P1402 酒店之王 网络流
大水题,我自己瞎做就做出来了,没啥说的,zz建图,就是板子. 题干: 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等, ...