結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー 37zigen37zigen
提出日時 2020-04-09 01:37:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 608 ms / 2,000 ms
コード長 3,576 bytes
コンパイル時間 2,812 ms
コンパイル使用メモリ 74,580 KB
実行使用メモリ 74,416 KB
最終ジャッジ日時 2023-09-26 11:35:37
合計ジャッジ時間 6,893 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 608 ms
59,900 KB
testcase_01 AC 542 ms
64,516 KB
testcase_02 AC 364 ms
67,148 KB
testcase_03 AC 285 ms
72,240 KB
testcase_04 AC 286 ms
74,416 KB
testcase_05 AC 266 ms
74,152 KB
testcase_06 AC 45 ms
49,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}

	int good(int pos, char[] cs) {
		int INF=Integer.MAX_VALUE/3;
		int ret=0;
		if (pos+4>cs.length) return INF;
		char[] target= {'g','o','o','d'};
		for (int i=pos;i<pos+4;++i) if(target[i-pos]!=cs[i]) ++ret;
		return ret;
	}
	
	int problem(int pos, char[] cs) {
		int INF=Integer.MAX_VALUE/3;
		int ret=0;
		if (pos+7>cs.length) return INF;
		char[] target= {'p','r','o','b','l','e','m'};
		for (int i=pos;i<pos+7;++i) if(target[i-pos]!=cs[i]) ++ret;
		return ret;
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		int T=sc.nextInt();
		for (int t=0;t<T;++t) {
			char[] cs=sc.next().toCharArray();
			int n=cs.length;
			int INF=Integer.MAX_VALUE/3;
			int ans=INF;
			int[] dp1=new int[n];
			int[] dp2=new int[n];
			int[] pre=new int[n];
			Arrays.fill(dp1, INF);
			Arrays.fill(dp2, INF);
			for (int i=0;i<n;++i) {
				dp1[i]=Math.min(i==0?INF:dp1[i-1],good(i,cs));
			}
			for (int i=n-1;i>=0;--i) {
				dp2[i]=Math.min(i+1==n?INF:dp2[i+1],problem(i,cs));
			}
			for (int i=0;i<n;++i) {
				pre[i]=(i==0?0:pre[i-1])+(problem(i, cs)==0?1:0);
			}
			for (int i=0;i+11<=n;++i) {
				ans=Math.min(ans, dp1[i]+dp2[i+4]+(i-7<0?0:pre[i-7]));
			}
			System.out.println(ans);
		}
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    
    public double nextDouble() { return Double.parseDouble(next());}
}
0