E - String of CCPC (字符串处理)(str.substr()的运用)

滴答滴答---题目链接 

BaoBao has just found a string  of length  consisting of 'C' and 'P' in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring  of  is "good", if and only if  'C', and  'P', where  denotes the -th character in string . The value of  is the number of different "good" substrings in . Two "good" substrings and  are different, if and only if .

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 . But everything comes with a cost. If it's the -th time for BaoBao to buy a character, he will have to spend  units of value.

The final value BaoBao obtains is the final value of  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 , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of string .

The second line contains the string  () consisting of 'C' and 'P'.

It's guaranteed that the sum of  over all test cases will not exceed .

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  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  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  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

题意:

给出一个长度为n的字符串,全部由'C'和'P'组成;

then,可以在原有字符串的基础上添加任意多个’C’或’P’,每次添加一个就要花费一些 valuevalue,第 i 次添加所花费的 value=i−1value=i−1;

在任意的地方只要组成”CCPC”就可以获得 1 单位的 valuevalue,问你到最后可以获得最多多少单位的 valuevalue。

题解:

观察一下就可以知道,任何添加两个及以上字符的行为都是没有价值增加的。

所以,最多只需要增加一个字符(第一个字符是“免费”的字符)。

先遍历把"CCPC"找出来,然后剩下去判断是否可以利用"CCC"或"CCP"或"CPC"来生成目标字符串"CCPC";

另外, 由于找到的"CCC"有可能是"CCCPC"的前三个字符,那么如果添加一个字符"P",就变成"CCPCPC",这样显然会破坏一个原本就存在的"CCPC",

这样的行为显然是不赚的,而且也会浪费你唯一一次免费字符的机会,所以还不如不加这个字符,所以遇到"CCC"时需要特判一下。

substr有2种用法: 
假设:string s = “0123456789”;

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = “567”

#include<bits/stdc++.h>
using namespace std;

int n;
string str;

int main()
{
    int T;
    for(cin>>T;T;T--)
    {
        cin>>n>>str;
        int value=0;
        bool flag=0;
        for(int i=0;i<n;i++)
        {
            if(str.substr(i,4)=="CCPC")
            {
                value++;
                i+=2;
                continue;
            }
            if(!flag)
            {
                string tmp=str.substr(i,3);
                if(tmp=="CCC" || tmp=="CCP" || tmp=="CPC")
                {
                    if(tmp=="CCC" && str.substr(i+1,4)=="CCPC") continue; //特判
                    value++;
                    flag=1;
                }
            }
        }
        cout<<value<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/chen_zan_yu_/article/details/83552443