Submission #50893982


Source Code Expand

Copy
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
import Data.Array.Unboxed
import Data.Bits
main :: IO ()
main = do
[n] <- bsGetLnInts
ss <- replicateM n $ do
l <- BS.getLine
return $ if isLower $ BS.last l then l else BS.init l
let ans = abc343g n ss
print ans
bsGetLnInts :: IO [Int]
bsGetLnInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
abc343g :: Int -> [BS.ByteString] -> Int
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List

import Data.Array.Unboxed
import Data.Bits

main :: IO ()
main = do
  [n] <- bsGetLnInts
  ss <- replicateM n $ do
    l <- BS.getLine
    return $ if isLower $ BS.last l then l else BS.init l
  let ans = abc343g n ss
  print ans

bsGetLnInts :: IO [Int]
bsGetLnInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

abc343g :: Int -> [BS.ByteString] -> Int
abc343g n ss = kick `seq` minimum [lens ! (pred $ 2 ^ succ nn, i) | i <- [0..nn]]
  where
-- 文字列どうしの重なり量を計測 ovs ! (i,j) Sjの末尾とSiの先頭の重なる長さ
    ovs :: UArray (Int,Int) Int
    ovs = listArray ((1,1),(n,n))
          [ if i == j then BS.length si else kmpSearch si sj t
          | (si,i) <- zip ss [1..], let t = kmpTable si
          , (sj,j) <- zip ss [1..]
          ]
-- 埋まってしまう(同一を含む)文字列を除去した、扱うべき背番号のリスト
    is = foldl' instep [1..n] [1..n]
    instep js i = if any prop js then delete i js else js
      where
        prop j = i /= j && ovs ! (i,j) == ovs ! (i,i)
-- 本当に扱う相手に絞った情報にする 0 始まりにもする
    nn = pred $ length is
    uf = listArray (0, nn) is :: UArray Int Int
    uovs i j = ovs ! (uf ! i, uf ! j)
-- TSPで最短文字列長を求める
    bnds = ((1,0), (pred $ 2 ^ succ nn, nn)) -- ビット集合, 末尾の番号
    lens = listArray bnds $ map lenF $ range bnds :: Array (Int,Int) Int
    lenF (bs, i)
      | not $ testBit bs i = maxBound  -- bsにiがない
      | popCount bs == 1 = uovs i i    -- iのみのとき、iの長さ
      | otherwise = uovs i i + minimum -- iの手前jを総当たりで、jまでの長さ+iの長さ-重なり長 の最小値
        [ lens ! (bs1, j) - uovs i j
        | let bs1 = clearBit bs i
        , j <- [0..nn], testBit bs1 j
        ]
    kick = foldl' kstep () $ range bnds
    kstep st i = lens ! i `seq` ()

kmpSearch :: BS.ByteString -> BS.ByteString -> UArray Int Int -> Int
kmpSearch pat text tbl = loop 0 0
  where
    plen = BS.length pat
    tlen = BS.length text
    loop m i
      | i == plen = i -- パターンを全て照合できた
      | m + i == tlen = i -- パターン途中でテキスト終端に到達した
      | BS.index pat i == BS.index text (m + i) = loop m (succ i) -- 次の文字へ
      | otherwise = loop (m + i - ti) (if i > 0 then ti else 0) -- リセット
      where
        ti = tbl ! i

kmpTable :: BS.ByteString -> UArray Int Int
kmpTable pat = tbl
  where
    plen = BS.length pat
    tbllist = -1 : 0 : loop 2 0
    tbl = listArray (0, pred plen) tbllist
    loop i j
      | i == plen = []
      | cond1 = j1 : loop (succ i) j1
      | j > 0 = loop i (tbllist !! j)
      | otherwise = j : loop (succ i) j
      where
        cond1 = BS.index pat (pred i) == BS.index pat j
        j1 = succ j

Submission Info

Submission Time
Task G - Compress Strings
User joetheshootingst
Language Haskell (GHC 9.4.5)
Score 0
Code Size 3025 Byte
Status TLE
Exec Time 5670 ms
Memory 2310772 KB

Compile Error

app/Main.hs:28:31: warning: [-Wtype-defaults]
    • Defaulting the type variable ‘a0’ to type ‘Integer’ in the following constraints
        (Num a0) arising from the literal ‘1’ at app/Main.hs:28:31
        (Enum a0)
          arising from the arithmetic sequence ‘1 .. ’
          at app/Main.hs:28:30-34
        (Eq a0) arising from a use of ‘==’ at app/Main.hs:27:18-19
    • In the expression: 1
      In the second argument of ‘zip’, namely ‘[1 .. ]’
      In the expression: zip ss [1 .. ]
   |
28 |           | (si,i) <- zip ss [1..], let t = kmpTable si
   |                               ^

app/Main.hs:52:11: warning: [-Wunused-matches]
    Defined but not used: ‘st’
   |
52 |     kstep st i = lens ! i `seq` ()
   |           ^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 22
TLE × 17
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 02_random2_21.txt, 02_random2_22.txt, 02_random2_23.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 7140 KB
00_sample_01.txt AC 2 ms 7188 KB
00_sample_02.txt AC 1 ms 7092 KB
01_random_00.txt AC 142 ms 44592 KB
01_random_01.txt AC 21 ms 16084 KB
01_random_02.txt AC 3 ms 9672 KB
01_random_03.txt AC 2 ms 7668 KB
02_random2_00.txt AC 41 ms 14340 KB
02_random2_01.txt AC 31 ms 14760 KB
02_random2_02.txt AC 31 ms 18788 KB
02_random2_03.txt AC 41 ms 15944 KB
02_random2_04.txt AC 41 ms 17504 KB
02_random2_05.txt AC 81 ms 27268 KB
02_random2_06.txt AC 41 ms 16484 KB
02_random2_07.txt AC 61 ms 22464 KB
02_random2_08.txt AC 61 ms 27520 KB
02_random2_09.txt AC 31 ms 14240 KB
02_random2_10.txt AC 41 ms 21932 KB
02_random2_11.txt AC 31 ms 18432 KB
02_random2_12.txt TLE 5667 ms 2310772 KB
02_random2_13.txt TLE 5670 ms 2259668 KB
02_random2_14.txt TLE 5662 ms 2306672 KB
02_random2_15.txt TLE 5662 ms 2255572 KB
02_random2_16.txt TLE 5658 ms 2306740 KB
02_random2_17.txt TLE 5656 ms 2256584 KB
02_random2_18.txt TLE 5656 ms 2309744 KB
02_random2_19.txt TLE 5655 ms 2262648 KB
02_random2_20.txt TLE 5653 ms 2261584 KB
02_random2_21.txt TLE 5654 ms 2255496 KB
02_random2_22.txt TLE 5660 ms 2306692 KB
02_random2_23.txt TLE 5655 ms 2259412 KB
03_random3_00.txt TLE 5658 ms 2307772 KB
03_random3_01.txt TLE 5661 ms 2307520 KB
03_random3_02.txt TLE 5656 ms 2307680 KB
04_handmade_00.txt AC 13 ms 7000 KB
04_handmade_01.txt AC 6 ms 7712 KB
04_handmade_02.txt AC 2 ms 7372 KB
04_handmade_03.txt TLE 5661 ms 2260636 KB
04_handmade_04.txt TLE 5668 ms 2306704 KB


2025-04-21 (Mon)
05:23:49 +00:00