ZOJ - 3985

BaoBao has just found a string (s) of length (n) consisting of ‘C’ and ‘P’ in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring (s_is_{i+1}s_{i+2}s_{i+3}) of (s) is “good”, if and only if (s_i = s_{i+1} = s_{i+3} =) ‘C’, and (s_{i+2} =) ‘P’, where (s_i) denotes the (i)-th character in string (s). The value of (s) is the number of different “good” substrings in (s). Two “good” substrings (s_is_{i+1}s_{i+2}s_{i+3}) and (s_js_{j+1}s_{j+2}s_{j+3}) are different, if and only if (i \ne j).
To make this string more valuable, BaoBao decides to buy some characters from a character store. Each time he can buy one ‘C’ or one ‘P’ from the store, and insert the character into any position in (s). But everything comes with a cost. If it’s the (i)-th time for BaoBao to buy a character, he will have to spend (i-1) units of value.
The final value BaoBao obtains is the final value of (s) minus the total cost of all the characters bought from the store. Please help BaoBao maximize the final value.
Input
There are multiple test cases. The first line of the input contains an integer (T), indicating the number of test cases. For each test case:
The first line contains an integer (n) ((1 \le n \le 2\times 10^5)), indicating the length of string (s).
The second line contains the string (s) ((|s| = n)) consisting of ‘C’ and ‘P’.
It’s guaranteed that the sum of (n) over all test cases will not exceed (10^6).
Output
For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.
Sample Input
3
3
CCC
5
CCCCP
4
CPCP
Sample Output
1
1
1
Hint
For the first sample test case, BaoBao can buy one ‘P’ (cost 0 value) and change (s) to “CCPC”. So the final value is 1 - 0 = 1.
For the second sample test case, BaoBao can buy one ‘C’ and one ‘P’ (cost 0 + 1 = 1 value) and change (s) to “CCPCCPC”. So the final value is 2 - 1 = 1.
For the third sample test case, BaoBao can buy one ‘C’ (cost 0 value) and change (s) to “CCPCP”. So the final value is 1 - 0 = 1.
It’s easy to prove that no strategies of buying and inserting characters can achieve a better result for the sample test cases.

添加一个字母最多只能多构造一个CCPC,所以至多添加一次,否则不划算。
只有CCPCPCCCC能通过添加一个字母变成CCPC
直接扫一遍,看有多少CCPC,还有CCPCPCCCC至多要一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

```cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
#define scanf scanf_s

string s;


int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int n;
cin >> n >> s;
int Ans = 0;
bool flag = false;
for (int i = 0; i < n; ++i) {
if (i + 4 < n) {
if (s.substr(i, 5) == "CCCPC") {
i += 3;
++Ans;
continue;
}
}
if (i + 3 < n) {
if (s.substr(i, 4) == "CCPC") {
i += 2;
++Ans;
continue;
}
}
if (i + 2 < n) {
string temp = s.substr(i, 3);
if (temp == "CCC" || temp == "CPC" || temp == "CCP") {
flag = true;
continue;
}
}
}
cout << (Ans + flag) << endl;
}

}