Skip to content

Instantly share code, notes, and snippets.

@oskimura
Created April 13, 2011 13:39
Show Gist options
  • Select an option

  • Save oskimura/917555 to your computer and use it in GitHub Desktop.

Select an option

Save oskimura/917555 to your computer and use it in GitHub Desktop.
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math#-}
{-# OPTIONS_GHC -funbox-strict-fields -fexcess-precision -monly-3-regs #-}
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Main where
import Control.Applicative
import Data.List
import Data.Function (on)
import Text.Printf
import Control.Monad
import Data.Tuple (swap)
import Data.Array
import qualified Data.Map
import Text.XFormat.Read
--import Control.Monad.State
import Data.Maybe
import qualified Data.Tree as T
splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : splitBy p (dropWhile p b)
where
(a, b) = break p xs
split :: String -> [String]
split = splitBy (=='/')
rr :: String -> Integer
rr = read
--input = lines <$> getContents
--input = lines <$> readFile "/Users/oskimura/prog/input.txt"
--input = lines <$> readFile "/Users/oskimura/Downloads/A-small-practice (3).in"
input = lines <$> readFile "/Users/oskimura/Downloads/A-large-practice (2).in"
slices = unfoldr . phi
where phi n xs | n < 1 || null xs = Nothing
| otherwise = Just $ splitAt n xs
toIS text = case fromJust . readf (Integer % String) $ text of
x :%: b -> (x,b)
output xs = unlines . zipWith (\ c n -> "Case #"++ show c ++": "++ show n) [1..] $ xs
put = writeFile "output.txt"
buildDir = T.unfoldTree phi
where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)
groupPath = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath = on (==) (fst . break ('\\'==))
buildTree = map T.levels . map buildDir . groupPath . map f . map split . sort
where f (x1:x2:xs) = (x1++x2) :xs
f as ns = genericLength . concat . g at $ nt
where
at = concat . buildTree $ (map ("/root"++) . nf $ as)
nt = concat . buildTree $ (map ("/root"++) . nf $ (as++ns))
nf x = if null x then [""] else x
g (x:xs) (y:ys) =
d : (g xs ys)
where d = (y\\x)
g [] ys = ys
g _ [] = []
g [] [] = []
main = do {count : nums <- input
; let loop xs | null xs = []
| otherwise = (f s1 s2) : (loop xs')
where
[n,m] = map rr . words . head $ xs
s1 = genericTake n $ (tail xs)
s2 = genericTake m . genericDrop n $ (tail xs)
xs' = genericDrop (n+m) (tail xs) in
put . output . loop $ nums
}
@oskimura

Copy link
Copy Markdown
Author

66行目の(as++ns)がポイント

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment