結果

問題 No.996 Phnom Penh
ユーザー Mayimg
提出日時 2020-02-22 19:26:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,557 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 198,664 KB
最終ジャッジ日時 2025-01-09 01:54:28
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
string doit (string s) {
  int n = (int) s.size();
  string res;
  for (int i = 0; i < n; i++) {
    if (s.substr(i, 5) == "phnom") {
      i += 4;
      res += "penh";
      ans++;
    } else {
      res += s[i];
    }
  }
  s = res;
  n = (int) s.size();
  bool found = false;
  res = "";
  for (int i = 0; i < n; i++) {
    if (s[i] == 'h' || s[i] == 'e') {
      found = true;
    }
    if (s[i] == 'e') res += 'h';
    else if (s[i] != 'h') res += s[i];
  }
  if (found) ans++;
  return res;
}
vector<int> doit2 (string s) {
  int n = (int) s.size();
  int cnt = 0;
  int phn = false;
  vector<int> res;
  for (int i = 0; i < n; i++) {
    if (s.substr(i, 3) == "phn") {
      cnt = 0;
      phn = true;
      i += 2;
      continue;
    }
    if (s.substr(i, 2) == "om" && phn) {
      cnt++;
      i += 1;
    } else {
      if (cnt > 0) res.push_back(cnt);
      phn = false;
      cnt = 0;
    }
  }
  if (cnt > 0) res.push_back(cnt);
  return res;
}
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  string s;
  cin >> s;
  for (int i = 0; i < 10; i++) s = doit(s);
  auto v = doit2(s);
  if (v.empty()) {
    int e = 0, h = 0;
    int n = (int) s.size();
    for (int i = 0; i < n; i++) {
      if (s[i] == 'e') e++;
      if (s[i] == 'h') h++;
    }
    if (e > 0) ans += 2;
    else if (h > 0) ans += 1;
  } else {
    for (int i : v) ans += i;
    ans += *max_element(v.begin(), v.end());
    ans++;
  }
  cout << ans << endl;
  return 0;
}
0