div4 题目

发布时间 2023-12-26 13:12:11作者: rw156

.Dashboard - Codeforces Round 898 (Div. 4) - Codeforces

1.Problem - G - Codeforces这一题我们首先进行分类讨论,因为情况很多

第一种情况当首尾存在一个B的情况时,我们可以吃掉所有的A,这个时候只需要计算出A的数量就可以

第二种情况是当首尾都是A,但是中间有连续的B的时候,这个时候我们也可以分别向左向右吃掉所有的A,次数为A的个数

第三种情况是首尾都是A,且中间没有连续的b,即只有一个单独B,这个时候要贪心选择A多的那一边吃完

 1 signed main() {
 2   cin >> _;
 3   //_ = 1;
 4   while (_--) {
 5     string s;
 6     cin >> s;
 7     // cin >> n;
 8     int n = s.length(), cnt = 0;
 9     
10     bool all = (s[0] == 'B' || s[n - 1] == 'B');
11 
12     for (int i = 0; i < n - 1; i++) {
13       if (s[i] == s[i + 1] && s[i] == 'B') all = true;
14     }
15     
16     vector<int> lens;
17     int curr = 0;
18     for (int i = 0; i < n; i++) {
19       if (s[i] == 'A')
20         curr++;
21       else {
22         if (curr != 0) lens.push_back(curr);
23         curr = 0;
24       }
25     }
26 
27     if (curr != 0) lens.push_back(curr);
28     sort(lens.begin(), lens.end());
29 
30     if (lens.empty()) {
31       cout << 0 << endl;
32       continue;
33     }
34 
35     int tot = 0;
36     if (all) tot += lens[0]; //这种情况不存在连续B且两端没B
37     //此时我们只能选择A多的那一段贪心吃掉
38     for (int i = 1; i < lens.size(); i++) {
39       tot += lens[i];
40     }
41     cout << tot << endl;
42   }
Code