結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー fmhrfmhr
提出日時 2015-07-29 08:18:36
言語 Go
(1.22.1)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,612 bytes
コンパイル時間 13,158 ms
コンパイル使用メモリ 243,324 KB
実行使用メモリ 27,904 KB
最終ジャッジ日時 2024-04-19 03:04:03
合計ジャッジ時間 11,816 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
5,248 KB
testcase_01 AC 66 ms
5,248 KB
testcase_02 AC 23 ms
9,216 KB
testcase_03 AC 37 ms
27,904 KB
testcase_04 AC 36 ms
27,904 KB
testcase_05 AC 36 ms
27,904 KB
testcase_06 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	a := nextInt()
	for i := 0; i < a; i++ {
		solve()
	}
}

var hamP [1000000]int // 各文字から始まる文字列と"problem"と"good"のハミング距離(コスト) ham()
var hamG [1000000]int
var hamPP [1000000]int // hamP[] の値のi以降の最小値
const MAXCOST = 100     // 文字列変換の最大コスト

func solve() {
	s := readLine()
	cost := MAXCOST

	// hamPP[]を更新するために逆順ループ
	// min()を使うので最初の比較対象にMAXCOSTを入れておく
	hamPP[len(s)-6] = MAXCOST
	for j := len(s) - 7; j >= 0; j-- {
		hamP[j] = ham(s[j:j+7], "problem")
		hamG[j] = ham(s[j:j+4], "good")
		hamPP[j] = min(hamP[j], hamPP[j+1])
	}

	// コストの更新
	pCost := 0 // good*problem の前にある"ploblem"を壊すためのコスト
	for j := 0; j < len(s)-10; j++ {
		// pCostの計算
		if j >= 7 {
			if hamP[j-7] == 0 {
				pCost++
			}
		}
		cost = min(cost, hamG[j]+hamPP[j+4]+pCost)
	}

	fmt.Println(cost)
	return
}

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func ham(s1, s2 string) int {
	c := 0
	for i := 0; i < len(s1); i++ {
		if s1[i] != s2[i] {
			c++
		}
	}
	return c
}

///////////////////////////////////////////////////
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
	buf := make([]byte, 0)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func nextInt() int {
	i, e := strconv.Atoi(readLine())
	if e != nil {
		panic(e)
	}
	return i
}
0