結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー そすうぽよそすうぽよ
提出日時 2021-05-27 01:00:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,515 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 202,568 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-04-23 18:14:32
合計ジャッジ時間 3,792 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 19 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 43 ms
6,784 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 47 ms
6,912 KB
testcase_11 AC 50 ms
6,944 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 27 ms
5,760 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 50 ms
6,912 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 19 ms
5,376 KB
testcase_23 AC 57 ms
7,040 KB
testcase_24 AC 61 ms
7,040 KB
testcase_25 AC 61 ms
7,168 KB
testcase_26 AC 56 ms
7,168 KB
testcase_27 AC 54 ms
7,168 KB
testcase_28 AC 54 ms
7,168 KB
testcase_29 AC 56 ms
7,040 KB
testcase_30 AC 55 ms
7,168 KB
testcase_31 AC 54 ms
7,168 KB
testcase_32 AC 58 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

//using namespace harudake;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i32 n;
  cin>>n;
  string s;
  cin>>s;
  // dp[state][length][first]
  i32 INF = 1e8;
  vector dp(3, vector(n+1, vi32(n, -INF)));
  rep(i,n) dp[0][0][i] = 0;
  string pon = "pon";
  rep2(len,1,n+1) {
    rep(i,n) {
      if (i + len > n) break;
      rep(nc,3) {
        i32 nnc = (nc+1)%3;
        dp[nc][len][i] = max(dp[nc][len][i], dp[nc][len-1][i]);
        if (s[i+len-1] != pon[nc]) continue;
        rep2(j,i,i+len-1) {
          i32 len_left = j-i+1;
          i32 len_innder = (i+len-2) - (j+1) + 1;
          dp[nnc][len][i] = max(dp[nnc][len][i], dp[nc][len_left][i] + dp[0][len_innder][j+1] + (nnc == 0));
        }
        dp[nnc][len][i] = max(dp[nnc][len][i], dp[nc][len-1][i] + (nnc == 0));
      }
    }
  }
  i32 mx = -1;
  rep2(i,1,n-1) {
    i32 left = dp[0][i][0];
    i32 right = dp[0][n-i][i];
    if (left && right) {
      mx = max(mx, left + right);
    }
  }
  if (mx >= 2) {
    cout<<(mx - 2)<<endl;
  } else {
    cout<<-1<<endl;
  }
  return 0;
}
0