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 = Fn1 + 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
 
将前面的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);
         }
    }
}
原文地址:https://www.cnblogs.com/liyinggang/p/5565516.html