結果

問題 No.1916 Making Palindrome on Gird
ユーザー tonyu0tonyu0
提出日時 2022-05-02 22:09:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 3,000 ms
コード長 1,695 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 95,288 KB
実行使用メモリ 66,972 KB
最終ジャッジ日時 2023-09-14 19:19:28
合計ジャッジ時間 3,307 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
7,836 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
7,588 KB
testcase_05 AC 2 ms
5,600 KB
testcase_06 AC 2 ms
7,752 KB
testcase_07 AC 2 ms
7,708 KB
testcase_08 AC 2 ms
7,912 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 3 ms
7,632 KB
testcase_12 AC 2 ms
5,496 KB
testcase_13 AC 22 ms
44,676 KB
testcase_14 AC 17 ms
41,480 KB
testcase_15 AC 24 ms
50,096 KB
testcase_16 AC 17 ms
35,592 KB
testcase_17 AC 36 ms
62,652 KB
testcase_18 AC 40 ms
66,972 KB
testcase_19 AC 39 ms
66,856 KB
testcase_20 AC 39 ms
66,888 KB
testcase_21 AC 40 ms
66,844 KB
testcase_22 AC 40 ms
66,920 KB
testcase_23 AC 29 ms
65,284 KB
testcase_24 AC 29 ms
65,192 KB
testcase_25 AC 28 ms
65,152 KB
testcase_26 AC 29 ms
65,188 KB
testcase_27 AC 23 ms
10,492 KB
testcase_28 AC 40 ms
66,832 KB
testcase_29 AC 39 ms
66,908 KB
testcase_30 AC 41 ms
66,852 KB
testcase_31 AC 40 ms
66,848 KB
testcase_32 AC 40 ms
66,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i];
  return os << '\n';
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (T &x : a) { is >> x; }
  return is;
}
constexpr long long MOD = 1000000007;

ll dp[202][202][202];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  cin >> s;
  // dp[a][b][c][d]
  // a+b = h+w-2 - (c+d)
  // len 1
  rep(a, 0, h) rep(b, 0, w) if ((a + b) * 2 == h + w - 2) dp[a][b][a] = 1;
  // len 2
  rep(a, 0, h) rep(b, 0, w) {
    if (a + 1 < h && (a + b) * 2 + 1 == h + w - 2 && s[a][b] == s[a + 1][b])
      dp[a][b][a + 1] = 1;
    if (b + 1 < w && (a + b) * 2 + 1 == h + w - 2 && s[a][b] == s[a][b + 1])
      dp[a][b][a] = 1;
  }
  // 長さ1or2の元 から 回文を両側に伸ばしていく
  // 対角線辺りから更新する
  rrep(m, 0, (h + w - 2) / 2 + 1) {
    // m=a+b=h+w-2-(c+d)
    rep(a, 0, h) {
      rep(c, 0, h) {
        int b = m - a, d = h + w - 2 - m - c;
        if (0 <= b && b < w && 0 <= d && d < w) {
          if (s[a][b] == s[c][d]) {
            (dp[a][b][c] += dp[a][b + 1][c - 1] + dp[a + 1][b][c - 1] +
                            dp[a + 1][b][c] + dp[a][b + 1][c]) %= MOD;
          }
        }
      }
    }
  }
  cout << dp[0][0][h - 1];

  return 0;
}
0