結果

問題 No.22 括弧の対応
ユーザー regerege
提出日時 2015-12-05 16:47:38
言語 F#
(F# 4.0)
結果
AC  
実行時間 560 ms / 5,000 ms
コード長 1,711 bytes
コンパイル時間 6,230 ms
コンパイル使用メモリ 198,276 KB
実行使用メモリ 61,680 KB
最終ジャッジ日時 2024-07-20 07:05:59
合計ジャッジ時間 14,373 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (199 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

open System.Collections
let s (a:BitArray) = seq [ for i in 0..(a.Length-1) -> a.Get i ]
let inline (@>>) (a:BitArray) n =
    Seq.append (seq[for i in 1..n -> false]) (s a)
    |> Seq.take a.Length
    |> Seq.toArray
    |> BitArray
let inline (@<<) (a:BitArray) n =
    Seq.append (s a |> Seq.skip n) (seq[for i in 1..n -> false])
    |> Seq.take a.Length
    |> Seq.toArray
    |> BitArray
let inline (@=) (a:BitArray) (b:BitArray) =
    if a.Length = b.Length then
        Seq.zip (s a) (s b)
        |> Seq.forall (fun (a:bool,b:bool) -> a = b)
    else false
let inline (@&) (a:BitArray) (b:BitArray) = let c = new BitArray(a) in c.And(b)
let inline (@|) (a:BitArray) (b:BitArray) = let c = new BitArray(a) in c.Or(b)
let inline (@^) (a:BitArray) (b:BitArray) = let c = new BitArray(a) in c.Xor(b)
let inline (@!) (a:BitArray) = let c = new BitArray(a) in c.Not()
let ``No.22 括弧の対応`` () =
    let (N,K) = let t = stdin.ReadLine().Split(' ') in int t.[0],int t.[1]
    let cnt (a:BitArray) = a |> Seq.cast |> Seq.map (fun b -> if b then 1 else 0) |> Seq.sum
    let trace s (b:BitArray) = b |> Seq.cast |> Seq.map (fun b -> if b then "1" else "0") |> Seq.reduce(+) |> printfn "%s: %s" s
    let mask = BitArray([| for _ in 1..N -> true |])
    let T = BitArray([| for i in 1..N -> i = K |])
    let A = stdin.ReadLine() |> Seq.map ((=)'(') |> Seq.toArray |> BitArray
    let B = (@!) A
    let rec f n a b =
        let c,d = (a @>> n) @& b, (b @<< n) @& a
        if cnt a = 0 && cnt b = 0 then failwith "syntax error"
        elif (c @& T) @= T then K - n
        elif (d @& T) @= T then K + n
        else f (n+2) (a @^ d) (b @^ c)
    f 1 A B
    |> printfn "%d"
``No.22 括弧の対応`` ()
0