結果

問題 No.22 括弧の対応
ユーザー e7a894e7a894
提出日時 2020-09-18 14:32:39
言語 Haskell
(9.8.2)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 661 bytes
コンパイル時間 3,250 ms
コンパイル使用メモリ 161,640 KB
実行使用メモリ 8,144 KB
最終ジャッジ日時 2023-09-27 13:50:38
合計ジャッジ時間 4,283 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,000 KB
testcase_01 AC 2 ms
7,024 KB
testcase_02 AC 2 ms
7,064 KB
testcase_03 AC 3 ms
7,660 KB
testcase_04 AC 2 ms
7,304 KB
testcase_05 AC 3 ms
7,244 KB
testcase_06 AC 2 ms
7,496 KB
testcase_07 AC 2 ms
7,632 KB
testcase_08 AC 2 ms
7,336 KB
testcase_09 AC 3 ms
7,792 KB
testcase_10 AC 2 ms
7,460 KB
testcase_11 AC 2 ms
7,344 KB
testcase_12 AC 3 ms
7,616 KB
testcase_13 AC 3 ms
8,144 KB
testcase_14 AC 2 ms
7,180 KB
testcase_15 AC 2 ms
7,596 KB
testcase_16 AC 2 ms
7,612 KB
testcase_17 AC 2 ms
7,012 KB
testcase_18 AC 2 ms
6,996 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Monad
import Data.List

main = do
  [n, k] <- map read . words <$> getLine :: IO [Int]
  s <- getLine
  
  let x = s !! (k - 1)
  let s' = if x == '(' then drop k s else take (k - 1) s
  let result = if x == '(' then checkL s' 0 0 else checkR (reverse s') 0 0
  
  putStrLn . show $ if x == '(' then result + k else (length s') - result + 1
  
checkL (c:cs) count no
  | c == '(' = checkL cs (count + 1) (no + 1)
  | c == ')' && count > 0 = checkL cs (count - 1) (no + 1)
  | otherwise = no + 1

checkR (c:cs) count no
  | c == ')' = checkR cs (count + 1) (no + 1)
  | c == '(' && count > 0 = checkR cs (count - 1) (no + 1)
  | otherwise = no + 1
0