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

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.

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.

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.

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".

Source

2015 ACM/ICPC Asia Regional Shenyang Online

将前面的f全部放到后面去然后判断,注意串中只能有  c f

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);
         }
    }
}
时间: 2024-10-14 16:16:04

hdu 5455(字符串处理)的相关文章

hdu 2721(字符串处理,位运算 暴力)

Persistent Bits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 116 Problem Description WhatNext Software creates sequence generators that they hope will produce

(字符串处理)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 坑题

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

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,

hdu 5455 (2015沈阳网赛 简单题) Fang Fang

题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不满足条件的,还有我被坑了的地方就是当输入很多f的时候,我尽然脑抽的 认为这是不满足条件的,注意这两点就行了,直接暴力 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 int

Fang Fang hdu 5455

http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意:判断字符串最少符合题意的个数,它是一个环.若c前面有f,则把f的个数都加到后面去.还有一个坑点是,会有其他字母,不止有c,f. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <alg

hdu 3973 字符串hash+线段树

http://acm.hdu.edu.cn/showproblem.php?pid=3973 Problem Description You are given some words {Wi}. Then our stupid AC will give you a very long string S. AC is stupid and always wants to know whether one substring from S exists in the given words {Wi}

hdu 4821 字符串hash+map判重 String (长春市赛区I题)

http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这几次hash军写错的变量--tmp=(j==m-1)?ah[j]:(ah[j]-ah[j-m]*base[m]);  外层循环变量是i,我写的字符串hash的几题都写成tmp=(i==0)? ah[j]:(ah[j]-ah[j-m]*base[m]); 二逼啊 题目大意: 给定一个字符串(最长10^

hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现

http://acm.hdu.edu.cn/showproblem.php?pid=4850 题意:构造长度为n的字符序列,使得>=4的子串只出现一次 其实最长只能构造出来26^4+4-1= 456979 的序列,大于该数的都是不可能的.构造方法,就是那种欧拉回路的序列,此题DFS会爆栈,手动扩展栈也可以AC...... 递归形式的开始WA了,没有细调就换非递归了,后来又想了想,虽然自己电脑上运行不了,但是先把长度按小的来,然后调试代码,然后在扩大,AC了,当时错在MOD,递归的MOD应该是26