hdu 5455(字符串处理)
Fang Fang
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1317 Accepted Submission(s): 548
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.
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.
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.
ffcfffcffcff
cffcfff
cffcff
cffcf
ffffcffcfff
cffcfffcffffcfffff
cff
cffc
Case #2: 2
Case #3: 2
Case #4: -1
Case #5: 2
Case #6: 4
Case #7: 1
Case #8: -1
Shift the string in the first test case, we will get the string "cffffcfffcff"
and it can be split into "cffff", "cfff" and "cff".
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tcase = sc.nextInt();
int t = 1;
while(tcase-->0){ String str = sc.next(),sub1="";
char [] s = str.toCharArray();
int i;
boolean havec = false;
for(i=0;i<s.length;i++){
if(s[i]!='c'&&s[i]!='f') break;
if(s[i]=='c') {havec=true;}
}
if(i!=s.length){
System.out.print("Case #"+(t++)+": ");
System.out.println(-1);
continue;
}
if(havec==false){
System.out.print("Case #"+(t++)+": ");
if(s.length%2==1){
System.out.println((s.length+1)/2);
}else{
System.out.println((s.length)/2);
}
continue;
}
i = 0;
while(i<s.length&&s[i]!='c'){
sub1+=s[i];
i++;
}
str = str.substring(i, s.length);
str+=sub1;
s= str.toCharArray();
boolean flag = false; ///c cf 不合法
int ans = 0;
for(i=0;i<s.length;){
int len = 0;
if(s[i]=='c'){
i++;
while(i<s.length&&s[i]!='c'){
len++;
i++;
}
}
if(len<2) {flag = true;break;}
ans++;
}
System.out.print("Case #"+(t++)+": ");
if(flag) System.out.println(-1);
else System.out.println(ans);
}
}
}
hdu 5455(字符串处理)的更多相关文章
- 单词数 HDU 2072 字符串输入控制
单词数 HDU 2072 字符串输入控制 题意 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个 ...
- (字符串处理)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 坑题
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 ...
- hdu 5455 (2015沈阳网赛 简单题) Fang Fang
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...
- hdu 4821 字符串hash+map判重 String (长春市赛区I题)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...
- hdu 1880 字符串hash
/*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...
- HDU 1880 字符串hash 入门题
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
- HDU 5455 Fang Fang 水题,但题意描述有问题
题目大意:f[1]=f,f[2]=ff,f[3]=ffc,以后f[n]每增加1,字符串增加一个c.给出一个字符串,求最少有多少个f[]组成.(字符串首尾相连,比如:ffcf可看做cfff) 题目思路: ...
随机推荐
- Unity基础-Input接口
input 底层的设备输入接口,在开发中很少用到 Input.GetKey() // Update is called once per frame void Update () { if (Inpu ...
- Linux菜鸟起飞之路【六】权限管理(二)
一.权限信息详解 ls -l 文件 //查看文件权限写法1 ll 文件 //查看文件权限写法2 ls -dl 目录 //查看目录权限写法1 ll -d 目录 //查看目录权限写法2 文件权限格式: ...
- 解决 viewer.js 动态更新图片导致无法预览的问题
1.viewer.js 使用 Demo http://fengyuanchen.github.io/viewerjs/ 2.viewer.js 下载地址 https://github.com/feng ...
- OOP面向对象形式的初使化配置
init.php里: <?php use ElemeOpenApi\Config\Config; define("BASE_DIR", dirname(__FILE__) . ...
- usb driver编写 (转)
在开头补上LDD3的一句话:如果 USB 驱动没有和另一种处理用户和设备交互的子系统(例如 input, tty, video, 等待)关联, 驱动可使用 USB 主编号为了使用传统的和用户空间之间的 ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- JS中如何操作数组
背景:随笔中所应用到的代码来自于上一篇随笔,MVC&JQuery如何根据List动态生成表格,部分代码不再重复. 代码如下: $("#btnTan").click(func ...
- 洛谷P1085不高兴的津津
- Java学习笔记1---JVM、JRE、JDK
jdk包含jre,jre包含jvm. 用java语言进行开发时,必须先装jdk: 只运行java程序,不进行开发时,可以只装jre. JVM 即Java Virtual machine,Java虚拟机 ...
- [POJ 1005] I Think I Need a Houseboat C++解题
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 81874 ...