結果

問題 No.8081 HQ9+
ユーザー a01sa01to
提出日時 2025-09-30 01:16:11
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 926 bytes
コンパイル時間 6,429 ms
コンパイル使用メモリ 279,592 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-30 01:16:19
合計ジャッジ時間 6,042 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

inline int sqrt(int x) {
  int y = sqrtl(x);
  while (y * y > x) --y;
  while ((y + 1) * (y + 1) <= x) ++y;
  return y;
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  for (char c : s)
    if (c == 'Q') ++cnt;
  int sq = sqrt(cnt);
  if (cnt != 0 && sq * sq == cnt) {
    if (s.size() % sq == 0) {
      string t = s.substr(0, s.size() / sq);
      bool ok = true;
      rep(i, sq) if (s.substr(i * t.size(), t.size()) != t) ok = false;
      if (t.contains('H')) ok = false;
      if (ok) {
        int cnt1 = 0;
        for (char c : t)
          if (c == 'Q') ++cnt1;
        if (cnt1 == sq) {
          cout << t << '\n';
          return 0;
        }
      }
    }
  }
  cout << -1 << '\n';
  return 0;
}
0