package test_5; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * No.279 木の数え上げ * kamipeipaa君は木が大好きですが,今日は文字列で遊んでいます。 * kamipeipaa君は文字列SSを並び替えたときに"tree"という * 部分文字列をいくつ作ることが可能か興味があります。 * 教えてあげてください。 */ public class Question_09_0531 { static final int LENGTH_MIN = 1; static final int LENGTH_MAX = (int)Math.pow(10, 6); static final String KEY_T = "t"; static final String KEY_R = "r"; static final String KEY_E = "e"; public static void main(String[] args) { InputStreamReader re = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(re); try { String kamipeipaa = br.readLine(); if (LengthJudg(kamipeipaa, LENGTH_MIN, LENGTH_MAX)) { int treeCount = 0; //それぞれの文字の個数を調べる("t","r","e") int cntT = kamipeipaa.replaceAll("[^" + KEY_T + "]", "").length(); int cntR = kamipeipaa.replaceAll("[^" + KEY_R + "]", "").length(); int cntE = kamipeipaa.replaceAll("[^" + KEY_E + "]", "").length(); while(true) { if (cntT >= 1 && cntR >= 1 && cntE >= 2) { treeCount++; cntT--; cntR--; cntE = cntE - 2; } else { break; } } System.out.println(treeCount); } else { System.out.println("文字列の長さが有効範囲外です"); } } catch (NumberFormatException e) { System.out.println("数字を入力して下さい。"); } catch (IOException e) { System.out.println("エラーが発生しました。"); } finally { try { re.close(); br.close(); } catch (IOException e) { System.out.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました"); } } } /** * 有効値判定 * @param str 判定する文字列 * @param max 最大値 * @param min 最小値 * @return 範囲内ならtrue,範囲外ならfalseを返す */ private static boolean LengthJudg(String str, int min, int max) { Boolean result = false; if (min <= str.length() && str.length() <= max) { result = true; } return result; } }