結果

問題 No.517 壊れたアクセサリー
ユーザー aimyaimy
提出日時 2017-05-29 15:22:13
言語 Haskell
(9.8.2)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 860 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 164,732 KB
実行使用メモリ 5,292 KB
最終ジャッジ日時 2023-10-21 16:55:15
合計ジャッジ時間 3,516 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,260 KB
testcase_01 AC 2 ms
5,256 KB
testcase_02 AC 2 ms
5,256 KB
testcase_03 AC 2 ms
5,264 KB
testcase_04 AC 2 ms
5,256 KB
testcase_05 AC 2 ms
5,256 KB
testcase_06 AC 3 ms
5,256 KB
testcase_07 AC 3 ms
5,256 KB
testcase_08 AC 2 ms
5,256 KB
testcase_09 AC 2 ms
5,260 KB
testcase_10 AC 2 ms
5,260 KB
testcase_11 AC 2 ms
5,260 KB
testcase_12 AC 2 ms
5,264 KB
testcase_13 AC 3 ms
5,280 KB
testcase_14 AC 2 ms
5,292 KB
testcase_15 AC 2 ms
5,292 KB
testcase_16 AC 2 ms
5,276 KB
testcase_17 AC 2 ms
5,288 KB
testcase_18 AC 2 ms
5,264 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Monad
import Data.Maybe
import Data.List

last' xs = if null xs then Nothing else Just (last xs)

main = do
 n <- readLn
 as <- replicateM n getLine
 m <- readLn
 bs <- replicateM m getLine
 putStrLn (maybe "-1" id (brokenAcc as bs))

brokenAcc as bs = do
 let as' = map (checkps bs) as
 guard (valid as')
 return (connect as')

checkps bs a = (a, (prev,next))
 where
  pre = head a
  suf = last a
  prev = find (elem pre) bs >>= last' . takeWhile (/= pre)
  next = find (elem suf) bs >>= last' . takeWhile (/= suf) . reverse

valid = (\(ps,ss) -> (nothing ps, nothing ss) == (1,1)) . unzip . map snd
 where nothing = length . filter isNothing

connect as = concat (unfoldr connect' (as,Nothing))
 where
  connect' ([],_) = Nothing
  connect' (as,next) = Just (a, (as',Just (last a)))
   where ([(a,(_,s))],as') = partition ((==next).fst.snd) as
0