結果

問題 No.346 チワワ数え上げ問題
ユーザー legosuke
提出日時 2017-12-10 05:19:04
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 558 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 159,704 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-30 07:34:30
合計ジャッジ時間 2,474 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(void) {
  string s;
  cin >> s;

  int sz = s.size();
  vector<int> cumc(sz, 0), cumw(sz, 0);
  for (int i = 0; i < sz; i++) {
    cumc[i] = (s[i] == 'c');
    if (i) cumc[i] += cumc[i - 1];
  }
  for (int i = sz - 1; i >= 0; i--) {
    cumw[i] = (s[i] == 'w');
    if (i != sz - 1) cumw[i] += cumw[i + 1];
  }

  ll ans = 0;
  for (int i = 0; i < sz; i++) {
    if (s[i] != 'w') continue;
    ans += (ll)cumc[i] * (max(0, cumw[i] - 1));
  }
  cout << ans << endl;

  return 0;
}
0