import Data.Function
import qualified Data.List as L
import qualified Data.Map as M
sp = '_'
alignColumn = L.transpose . f . L.transpose . addsp
where
add xs
= xs
++ replicate
(maxlen
- length xs
) sp
f [] = []
f xss = g $ do
is
<- groupOfIndex
$ head xss
let (ys:yss) = insertSP xss is
g [] = []
insertSP xss1 is = f xss1 pre
where
pre
= zipWith (\k
_ -> if elem k is
then Nothing
else Just sp
) [0..] (head xss1
) f [] cs
| all (== Nothing
) cs
= [] where
where
g x Nothing = (x, Nothing)
g x (Just c) = (c, Just x)
groupOfIndex xs
= map (spi
++) . M
.elems
. fst . foldl f
(M
.empty
, 0) $ xs
where
spi = L.findIndices (==sp) xs
f (acc,i) x
| x == sp = (acc, i+1)
| otherwise = (M
.insertWith
(++) x
[i
] acc
, i
+1)
main = do
run ["1","1"]
run ["1","2"]
run ["12", "21"]
run ["11", "12"]
run ["113", "114"]
run ["123", "134"]
run ["1212", "1322", "1122"]
run ["123", "113", "1323"]
run ["1234", "4321", "1122"]
-- run ["12121313432121233", "21322312443213443"]
where
run xs = do
aW1wb3J0IERhdGEuRnVuY3Rpb24KaW1wb3J0IHF1YWxpZmllZCBEYXRhLkxpc3QgYXMgTAppbXBvcnQgcXVhbGlmaWVkIERhdGEuTWFwICBhcyBNCgpzcCA6OiBDaGFyCnNwID0gJ18nCgphbGlnbkNvbHVtbiA6OiBbU3RyaW5nXSAtPiBbU3RyaW5nXQphbGlnbkNvbHVtbiA9IEwudHJhbnNwb3NlIC4gZiAuIEwudHJhbnNwb3NlIC4gYWRkc3AKICAgIHdoZXJlCiAgICAgIGFkZHNwIHhzcyA9IG1hcCBhZGQgeHNzCiAgICAgICAgICB3aGVyZSBtYXhsZW4gPSBmb2xkciAoXHggYSAtPiBpZiBsZW5ndGggeCA+IGEgdGhlbiBsZW5ndGggeCBlbHNlIGEpIDAgeHNzCiAgICAgICAgICAgICAgICBhZGQgeHMgPSB4cyArKyByZXBsaWNhdGUgKG1heGxlbiAtIGxlbmd0aCB4cykgc3AKICAg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