提出 #76653491
ソースコード 拡げる
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e5 + 5;
int dp[N][3][12];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int tt;
cin >> tt;
while(tt--)
{
string s;
cin >> s;
int n = s.size(), k;
cin >> k;
for(int i = 0; i < n; i++)
for(int x = 0; x < 3; x++)
for(int j = 0; j < 12; j++)
dp[i][x][j] = N;
dp[1][0][0] = 0;
dp[1][1][0] = (s[1] != 'A');
dp[1][2][0] = (s[0] != 'A') + (s[1] != 'B');
for(int i = 1; i + 1 < n; i++)
{
for(int j = 0; j <= k; j++)
{
dp[i + 1][0][j] = min(dp[i + 1][0][j], min({dp[i][0][j], dp[i][1][j], dp[i][2][j]}));
if(s[i + 1] == 'A')
dp[i + 1][1][j] = min(dp[i + 1][1][j], min({dp[i][0][j], dp[i][1][j], dp[i][2][j]}));
if(s[i + 1] == 'B')
dp[i + 1][2][j] = min(dp[i + 1][2][j], dp[i][1][j]);
if(s[i + 1] == 'C')
dp[i + 1][0][j + 1] = min(dp[i + 1][0][j + 1], dp[i][2][j]);
dp[i + 1][1][j] = min(dp[i + 1][1][j], min({dp[i][0][j], dp[i][1][j], dp[i][2][j]}) + 1);
dp[i + 1][2][j] = min(dp[i + 1][2][j], dp[i][1][j] + 1);
dp[i + 1][0][j + 1] = min(dp[i + 1][0][j + 1], dp[i][2][j] + 1);
}
if(s[i - 1] == 'A' && s[i] == 'B' && s[i + 1] == 'C')
{
for(int x = 0; x < 3; x++)
{
for(int j = 0; j <= k; j++)
dp[i + 1][x][j] = dp[i + 1][x][j + 1];
}
}
}
int ans = min({dp[n - 1][0][k], dp[n - 1][1][k], dp[n - 1][2][k]});
if(ans > n)
ans = -1;
cout << ans << '\n';
}
return 0;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | F - More ABC |
| ユーザ | aaa65654 |
| 言語 | C++23 (GCC 15.2.0) |
| 得点 | 500 |
| コード長 | 1940 Byte |
| 結果 | AC |
| 実行時間 | 27 ms |
| メモリ | 46036 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 500 / 500 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | example_00.txt |
| All | example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| example_00.txt | AC | 1 ms | 3460 KiB |
| hand_00.txt | AC | 27 ms | 45968 KiB |
| hand_01.txt | AC | 1 ms | 3612 KiB |
| hand_02.txt | AC | 1 ms | 3572 KiB |
| hand_03.txt | AC | 25 ms | 45916 KiB |
| hand_04.txt | AC | 21 ms | 46036 KiB |
| hand_05.txt | AC | 19 ms | 46032 KiB |
| hand_06.txt | AC | 18 ms | 45964 KiB |
| hand_07.txt | AC | 9 ms | 3516 KiB |
| hand_08.txt | AC | 8 ms | 3600 KiB |
| hand_09.txt | AC | 1 ms | 3452 KiB |
| random_00.txt | AC | 9 ms | 3572 KiB |
| random_01.txt | AC | 10 ms | 3604 KiB |
| random_02.txt | AC | 9 ms | 3592 KiB |
| random_03.txt | AC | 9 ms | 3696 KiB |
| random_04.txt | AC | 9 ms | 3476 KiB |
| random_05.txt | AC | 9 ms | 4912 KiB |
| random_06.txt | AC | 9 ms | 5048 KiB |
| random_07.txt | AC | 9 ms | 5080 KiB |
| random_08.txt | AC | 9 ms | 5064 KiB |
| random_09.txt | AC | 9 ms | 4884 KiB |
| random_10.txt | AC | 24 ms | 45696 KiB |
| random_11.txt | AC | 17 ms | 45676 KiB |
| random_12.txt | AC | 22 ms | 45664 KiB |
| random_13.txt | AC | 24 ms | 45900 KiB |
| random_14.txt | AC | 21 ms | 45776 KiB |
| random_15.txt | AC | 19 ms | 45668 KiB |
| random_16.txt | AC | 24 ms | 45664 KiB |
| random_17.txt | AC | 23 ms | 45704 KiB |
| random_18.txt | AC | 22 ms | 45904 KiB |
| random_19.txt | AC | 22 ms | 45592 KiB |