結果

問題 No.380 悪の台本
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 17:12:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 569 ms / 1,000 ms
コード長 2,767 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 78,172 KB
実行使用メモリ 64,780 KB
最終ジャッジ日時 2023-10-17 07:22:35
合計ジャッジ時間 4,787 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,580 KB
testcase_01 AC 58 ms
53,576 KB
testcase_02 AC 56 ms
53,576 KB
testcase_03 AC 56 ms
53,568 KB
testcase_04 AC 185 ms
56,716 KB
testcase_05 AC 254 ms
58,940 KB
testcase_06 AC 248 ms
58,812 KB
testcase_07 AC 569 ms
64,780 KB
testcase_08 AC 112 ms
55,280 KB
testcase_09 AC 196 ms
56,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = stdin.readLine()) != null) {
            String ans = isOK(line) ? "CORRECT (maybe)" : "WRONG!";
            System.out.println(ans);
        }
    }
    
    public static Set<Character> letters = new HashSet<>();
    
    public static boolean isOK(String line) {
        if (letters.isEmpty()) {
            for (char ch = 'a'; ch <= 'z'; ch++) letters.add(ch);
            for (char ch = 'A'; ch <= 'Z'; ch++) letters.add(ch);
            for (char ch = '0'; ch <= '9'; ch++) letters.add(ch);
        }
        
        // 1行を最大2つに分割。分割できない場合は異常。
        String[] tokens = line.split(" ", 2);
        if (tokens.length != 2) {
            return false;
        }
        
        String who = tokens[0];
        String speech = tokens[1].toLowerCase();
        
        // digiの全てのセリフはnyoか,nyoの末尾に記号を1~3文字つけたもので終わる
        // petitの全てのセリフはnyuか,nyuの末尾に記号を1~3文字つけたもので終わる
        // gemaの全てのセリフはgemaか,gemaの末尾に記号を1~3文字つけたもので終わる
        // piyoの全てのセリフはpyoか,pyoの末尾に記号を1~3文字つけたもので終わる
        if (who.equals("digi"))  return endsWith(speech, "nyo");
        if (who.equals("petit")) return endsWith(speech, "nyu");
        if (who.equals("gema"))  return endsWith(speech, "gema");
        if (who.equals("piyo"))  return endsWith(speech, "pyo");
        
        // rabiの全てのセリフは記号でない文字を少なくても1文字含む
        if (who.equals("rabi")) {
            for (char ch : speech.toCharArray()) {
                if (letters.contains(ch)) return true;
            }
            return false;
        }
        
        // だれともマッチしなかった。
        return false;
    }
    
    public static boolean endsWith(String word, String suffix) {
        // 最大3個まで末尾の記号を除去する。
        for (int i = 0; i < 3; i++) {
            if (word.isEmpty()) {
                break;
            }
            
            char c = word.charAt(word.length() - 1);
            if (letters.contains(c)) {
                break;
            }
            
            word = word.substring(0, word.length() - 1);
        }
        
        return word.endsWith(suffix);
    }
}
0