2018-05-01から1ヶ月間の記事一覧

12.3 深さ優先探索(写経編)

間が空いた上に写経という。書籍内の回答例は変数名の意味が分かりづらくて辛いので、そこだけ修正しました。 #include <iostream> #include <vector> #include <stack> #include <algorithm> using namespace std; enum COLOR { WHITE, GRAY, BLACK, }; static const int NIL = -1; static const</algorithm></stack></vector></iostream>…

12.2 グラフの表現

これは特に悩むところはなく。これから難しくなると思うので小休憩ですね。 #include <iostream> using namespace std; static const int N = 100; static int M[N + 1][N + 1] = {}, p[N + 1] = {}; int main() { int n; cin >> n; int number, count, value; for (in</iostream>…

11.4 連鎖行列積(写経編)

これも写経です。線形代数の確かな知識がないと、手も足も出ませんね。。 写経だけだとアレなので、boost の matrix を使おうと思いましたが、オンラインジャッジで boost が使えるのかがよく分からなかったのでそのままにしておきました。 #include <iostream> #inclu</iostream>…

11.3 最長共通部分列

またもや写経です。 2つの文字列の最大長を二次元配列で記憶する 文字一致ループの中で1つ前の最大長と比較していく ということですが、この発想が出てこない。。 #include <iostream> #include <string> #include <algorithm> using namespace std; static const int N = 1000; static </algorithm></string></iostream>…

11.2 フィボナッチ数列

動的計画法の基本とのことです。特に難しいところはなし。 #include <iostream> #include <vector> using namespace std; static vector<int> M(44, 0); static inline int fibonacci(int n) { if (n == 0 || n == 1) { return 1; } if (M[n] != 0) { return M[n]; } M[n] = fibonac</int></vector></iostream>…