2012-01-01から1年間の記事一覧

深さ優先探索によるトポロジカルソート

Lua

トポロジカルソートのアルゴリズムは、『プログラマのうちあけ話―続・プログラム設計の着想』を読んで学びました。この本では次の方法により、トポロジカルソートします。 入力辺がないノードをキュー q に入れる while (q に要素が含まれる): node = q.pop(…

池袋バイナリ勉強会(9)に参加しました

12月2日(日)に開催された池袋バイナリ勉強会(9)に参加しました。PDP-11のバイナリ解析に取り組んでいます。a.outの逆アセンブル、およびインタプリタの作成に取り組んでいます。 目標: hello.c を実行するインタプリタの作成 % cat hello.c main() { write(1…

SRM563 div2 easy FoxAndHandleEasy

SRM

文字列Sがあり、その文字列Sの任意の位置に同じ文字列Sを挿入する。そうして作られる文字列をSのexpansionと呼ぶとする。文字列S, Tが与えられたとき、TがSのexpansionかどうかを求める問題。 コンテスト中に書いたコードは以下。std::stringの文字列の削除…

池袋バイナリ勉強会(8)に参加しました

11月25日(日)に開催された池袋バイナリ勉強会(8)に参加しました。PDP-11のバイナリ解析に取り組んでいます。a.outを逆アセンブルする自作スクリプトを作成しながら、平行してそれを実行するインタプリタを作ろうとしている段階です。 逆アセンブラの作成 以…

池袋バイナリ勉強会(5)に参加しました

10月21日(日)に開催された池袋バイナリ勉強会(5)に参加しました。PDP-11の実行ファイル(a.out形式)の解析に取り組んでいます。 以下、取り組んだ内容のメモです(書かれている内容には間違いがあるかも知れません)。 前回までの復習 前回までの勉強会で学んだ…

池袋バイナリ勉強会(2)に参加してきました

9月23日(日)に池袋バイナリ勉強会(2)に参加してきました。 MonoDevelopでHello world! MonoDevelopとGTK#を用いて、ボタンをクリックしたらメッセージダイアログを表示するGUIプログラムを書きました。 MonoDevelopをインストールします。MonoDevelopからダ…

Codeforces 110B - B. Lucky String

110B - B. Lucky String calc :: Int -> String calc n = take n $ cycle "abcd" main = do s <- getLine putStrLn $ calc $ read s

Processing, プロジェクト名と同じ名前のクラスは定義できない。

プロジェクト名(.pdeファイル名)と同じ名前のクラスを定義しようとすると、以下のエラーメッセージが表示されました。 The nested type Test cannot hide an enclosing type

Processingで正三角形を描画する

Processingで正三角形を描画しようと思い、コードを書き始めたけど、すぐに手が止まってしまった。triangle()を使って、3点の座標位置を指定すれば三角形を描画できる。だけど、3点の座標位置はどう決めたら良いのだろうか?そもそも正三角形とはどういう形…

primesパッケージをインストール

primesパッケージをインストールします。 cabal install primes http://hackage.haskell.org/package/primes Prelude Data.Numbers.Primes> take 30 primes [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113] …

Codeforces 37A - A. Towers

37A - A. Towers import Data.List calc :: [Int] -> (Int, Int) calc xs = (maximum [count x xs | x <- xs], length $ nub xs) where count a xs = sum [1 | x <- xs, x == a] main = do s <- getLine t <- getLine let (x, y) = calc $ map read $ words …

Codeforces 122B - B. Lucky Substring

122B - B. Lucky Substring calc :: String -> Int calc s | n4+n7 == 0 = -1 | otherwise = if n4 >= n7 then 4 else 7 where n4 = sum [1 | c <- s, c == '4'] n7 = sum [1 | c <- s, c == '7'] main = do s <- getLine print $ calc s

Codeforces 122A - A. Lucky Division

122A - A. Lucky Division isLucky :: Int -> Bool isLucky n = show n == filter (\c -> c == '4' || c == '7') (show n) calc :: Int -> String calc n = if null [i | i <- [1..n], isLucky i, n `mod` i == 0] then "NO" else "YES" main = do s <- getL…

第4回 スタートHaskell2に参加してきました

第4回 スタートHaskell2 - [PARTAKE] に参加してきました。 第8章「入出力」@igrep さん IO action putStrLn は文字列を引数にとり、()(空のタプル。unit型ともいう)を結果とする I/O アクションを返す do記法。IO actionをつなぎ合わせる。 >>= 関数の構文…

Codeforces 146B - B. Lucky Mask

146B - B. Lucky Mask整数 a, b が与えられたとき、c のマスクが b と等しく、かつ c > a であるような c を求めよという問題。「c のマスクが b と等しい」とは、整数 c から 4 と 7 以外の数字を取り除いた数が b と等しいという意味。制約: 1 全探索。 ma…

Codeforces 221A - A. Little Elephant and Function

221A - A. Little Elephant and Function calc :: Int -> [Int] calc 1 = [1] calc 2 = [2, 1] calc n = [n, 1] ++ [2..n-1] main = do s <- getLine putStrLn $ unwords $ map show $ calc $ read s

Codeforces 26A - A. Almost Prime

26A - A. Almost Prime素因数分解したときに、素因数の種類が 2 つかどうか求める問題。 import Data.List factorize :: Int -> [Int] factorize 1 = [1] factorize n = fac 2 n where fac p n | n == 1 = [] | n `mod` p == 0 = p : fac p (n `div` p) | ot…

Codeforces 148A - A. Insomnia cure

148A - A. Insomnia cure calc :: Int -> Int -> Int -> Int -> Int -> Int calc k l m n d = d - length [i | i <- [1..d], i `mod` k /= 0, i `mod` l /= 0, i `mod` m /= 0, i `mod` n /= 0] main = do s <- getContents let [k, l, m, n, d] = map read …

Codeforces 3A - A. Shortest path of the king

3A - A. Shortest path of the king import Data.Char parse :: String -> (Int, Int) parse s = (digitToInt (s!!1), ord (s!!0) - ord 'a' + 1) calc :: (Int, Int) -> (Int, Int) -> [String] calc (sr, sc) (dr, dc) | sr == dr && sc == dc = [] | othe…

Codeforces 139A - A. Petr and Book

139A - A. Petr and Book calc :: Int -> [Int] -> Int calc n pages = calc' n (cycle pages) (cycle [1..7]) where calc' n (p:pages) (d:dayWeek) | n <= p = d | otherwise = calc' (n-p) pages dayWeek main = do s <- getLine t <- getLine print $ ca…

Codeforces 146A - A. Lucky Ticket

146A - A. Lucky Ticket import Data.Char isLucky :: String -> Bool isLucky s = all (\c -> c == '4' || c == '7') s calc :: String -> String calc s = if isLucky s && sum hd == sum tl then "YES" else "NO" where xs = map digitToInt s n = length…

Codeforces 219A - A. k-String

219A - A. k-String 文字列をソートする。 同じ文字でグルーピング。 グルーピングした文字列の長さが k の倍数なら、k-string は作れる。そうでなければ k-string は作れない import Data.List calc :: Int -> String -> Maybe String calc k s = if all (\…

Codeforces 50A - A. Domino piling

50A - A. Domino pilingM x N サイズのボードに 2 x 1 サイズのドミノを出来るだけ沢山敷き詰める問題。ボードに配置可能なドミノの個数を出力する。制約: 1 ボードのサイズが偶数であれば、隙間なくドミノを敷き詰められます。なので、まず偶数サイズのボー…

Codeforces 189A - A. Cut Ribbon

189A - A. Cut Ribbon長さ n のリボンある。そのリボンを出来るだけたくさんカットしたい。カットされたリボンの長さは、a, b, cのいずれかでなければならない。最大でいくつのリボンにカットできるか、という問題。制約: 1 DPの問題なのでC++で解きました。…

Codeforces 4A - A. Watermelon

4A - A. Watermelon calc :: Int -> String calc n | n > 2 && even n = "YES" | otherwise = "NO" main = do s <- getLine putStrLn $ calc $ read s

Haskellのdoの中のif式のインデント

Haskellについて調べていたら、do の中の if 式の then と else は if よりもインデントを深くする必要があるという情報があった。手元で試す限り、if, then, elseを揃えても特にエラーにならない。仕様が変わったのだろうか。もしかしたらと思い、-Wall を…

Codeforces 208A - A. Dubstep

208A - A. Dubstep WUBを空白に置き換える 連続する空白をひとつの空白にまとめる 両端の空白を取り除く -- "WUB"を取り除く(代わりに空白をおく) removeWUB :: String -> String removeWUB "" = "" removeWUB ('W':'U':'B':xs) = ' ' : removeWUB xs remove…

Codeforces 1A - A. Theatre Square

1A - A. Theatre Square calc n m a = x * y where x = (n `div` a) + (signum $ n `mod` a) y = (m `div` a) + (signum $ m `mod` a) main = do s <- getLine let [n, m, a] = map read $ words s :: [Integer] print $ calc n m a signumを括弧で囲まない…

Codeforces 216A - A. Tiling with Hexagons

216A - Tiling with Hexagonsb*cを一つの固まりとみなす。aの値が増減するとき、b*cの固まりを取り除いた図形のパターンがどのように変換するかを考えた。b*cの固まりを取り除くと、以下の「へ」の字のラインが見えてくる。 calc a b c = (a-1) * (b + c - 1…

fizzbuzz

http://chaton.practical-scheme.net/gauche/a/2012/08/09#entry-5023d040-7e774 のPythonコードが分かりやすかったので、Haskellで書いてみました。 fizz = cycle ["", "", "fizz"] buzz = cycle ["", "", "", "", "buzz"] fizzbuzz n = [if xy == "" then …