結果
| 問題 |
No.2738 CPC To F
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-20 17:43:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 17 ms / 2,000 ms |
| コード長 | 3,453 bytes |
| コンパイル時間 | 1,245 ms |
| コンパイル使用メモリ | 117,472 KB |
| 最終ジャッジ日時 | 2025-02-21 07:05:42 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <iomanip>
constexpr int MAX_INT = 2147483647;
constexpr int MIN_INT = -2147483648;
#define rep(n, a, b) for (int (n) = (a); (n) < (b); (n)++)
#define rrep(n, a, b) for (int (n) = (a); (n) >= (b); (n)--)
#define llrep(n, a, b) for (long long (n) = (a); (n) < (b); (n)++)
#define llrrep(n, a, b) for (long long (n) = (a); (n) >= (b); (n)--)
#define itrAll(x) (x).begin(), (x).end()
#define inputvec(v) for (auto& x : v) {std::cin >> x;}
using namespace std;
using uint = unsigned int;
using ushort = unsigned short;
using byte = unsigned char;
using ll = long long;
using ull = unsigned long long;
using ldouble = long double;
using ipair = pair<int, int>;
using llpair = pair<long long, long long>;
int main() {
int N;
string S;
cin >> N >> S;
vector<ipair> cpc; // { 開始インデックス, 繰り返し回数 }
rep(i, 0, N - 2) {
if (S[i] == 'C') {
int r_cnt = 0;
for (int j = i + 1; j < N - 1; j += 2) {
if (S[j] == 'P' && S[j + 1] == 'C') {
r_cnt++;
}
else {
break;
}
}
if (r_cnt) {
cpc.push_back({ i, r_cnt });
i += (r_cnt << 1);
}
}
}
if (!cpc.size()) {
cout << 0 << endl;
return 0;
}
/// <summary>
/// [i] := {
/// [first]: i+1番目のCPC...PCまでの部分文字列のうち, 少なくともi番目の「最後の」CPCを変換したときのCPCTFの個数の最大値.
/// [second]: 〃のうち, i番目のCPCを変換していないときの〃.
/// }
/// </summary>
vector<ipair> cnt(cpc.size());
cnt[0] = { 0,0 };
for (int i = 1; i < cpc.size(); i++) {
auto& prev = cpc[i - 1];
auto& current = cpc[i];
ipair prevIdx = { prev.first, prev.first + (prev.second << 1) }, currentIdx = { current.first, current.first + (current.second << 1) };
int margin = currentIdx.first - prevIdx.second - 1;
if (margin == 1 && S[currentIdx.first - 1] == 'T') { // [..CPC]T[CPC..]
if (current.second <= 2) {
cnt[i] = { cnt[i - 1].second + 1, max(cnt[i - 1].first, cnt[i - 1].second) };
}
else {
int v = max(cnt[i - 1].first, cnt[i - 1].second + 1);
cnt[i] = { v,v };
}
}
else if (margin >= 2 && S[prevIdx.second + 1] == 'T' && S[prevIdx.second + 2] == 'F') { // [..CPC]TF..[CPC..]
int v = max(cnt[i - 1].first, cnt[i - 1].second + 1);
cnt[i] = { v,v };
}
else { // [..CPC][CPC..] or [..CPC]..[CPC..]
int v = max(cnt[i - 1].first, cnt[i - 1].second);
cnt[i] = { v,v };
}
}
auto& lastCpc = *(cpc.end() - 1);
ipair lastIdx = { lastCpc.first, lastCpc.first + (lastCpc.second << 1) };
auto& c = *(cnt.end() - 1);
if (lastIdx.second + 2 < N && S[lastIdx.second + 1] == 'T' && S[lastIdx.second + 2] == 'F') {
cout << max(c.first, c.second + 1) << endl;
}
else {
cout << max(c.first, c.second) << endl;
}
return 0;
}