結果

問題 No.684 Prefix Parenthesis
ユーザー ShadowMas
提出日時 2025-03-05 19:58:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 844 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 159,948 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-03-05 19:58:58
合計ジャッジ時間 2,558 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 6 WA * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:13:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |   scanf("%d%s", &n, s + 1);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define MAXN 100005
using namespace std;
typedef long long LL;
int n, dp[MAXN], ans, m[128];
char s[MAXN], c, p;
int main()
{
#ifndef ONLINE_JUDGE
  freopen("data/data1.in", "r", stdin);
  // freopen("data/data1.out", "w", stdout);
#endif
  scanf("%d%s", &n, s + 1);
  // n = strlen(s + 1);
  m['('] = -1, m[')'] = 1, m['['] = -2, m[']'] = 2;
  for (int i = 1; s[i]; i++)
  {
    c = s[i], p = s[i - 1];
    if (m[c] < 0)
      continue;
    if (m[c] + m[p] == 0)
      dp[i] = dp[i - 2] + 2;
    else if (m[p] > 0 && i - dp[i - 1] - 1 >= 1 && m[c] + m[s[i - dp[i - 1] - 1]] == 0)
      dp[i] = dp[i - 1] + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0) + 2;
    ans = max(ans, dp[i]);
  }
  // int cnt = count(dp + 1, dp + 1 + n, ans);
  // printf("%d %d", ans, ans ? cnt : 1);
  printf("%d", ans);
  return 0;
}
0