View Code of Problem 3687

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		int t =in.nextInt();
		for(int i =0;i<t;i++) {
			boolean bl=true;
			StringBuffer sb = new StringBuffer(in.next());
			int time=0;
			while(sb.charAt(0)=='F') {
				sb.append(sb.charAt(0));
				sb.deleteCharAt(0);
				time++;
				if(time>sb.length()) {
					bl=false;
					break;
				}
			}
			if(bl) {
				ArrayList<StringBuffer> al =new ArrayList<StringBuffer>();
				StringBuffer sb0 = new StringBuffer();
				sb0.append(sb.charAt(0));
				for(int j=1;j<sb.length();j++) {
					if(sb.charAt(j)=='C') {
						al.add(sb0);
						sb0.delete(0, sb0.length());
					}
					sb0.append(sb.charAt(j));
				}
				al.add(sb0);
				int count=al.size();
				for(StringBuffer sb1:al) {
					if(sb1.toString().equals("CF") || sb1.toString().equals("C")) {
						count=-1;
					}
				}
				System.out.println("Case #"+(i+1)+": "+count);
			}else {
				if(sb.length()==2 || sb.length()==1) {
					System.out.println("Case #"+(i+1)+": "+1);
				}else {
					System.out.println("Case #"+(i+1)+": "+-1);
				}
			}
		}
	}
}

Double click to view unformatted code.


Back to problem 3687