1. 程式人生 > >E - String of CCPC (字串處理)(str.substr()的運用)

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