String of CCPC 思维,字符串,细节

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.

 

 

 

 有一个只有包含C和P两种字符的字符串,问只在该字符串某一个位置插入一个'C'或'P',最多可使该字符串中含有多少个"CCPC",可以两个CCPC共用同一个C,如CCPCCPC算是有两个

题目中说可以插入多个字符,但一旦插入的字符大于1,那么反而会亏本。所以在判断题目给的串有多少个ccpc后,只要

再考虑插入一个c或者p后是否能形成ccpc这一种情况就行。而能插入一个字符就成功的只有ccc,cpc,ccp三种情况,而且

在ccc的检查中国后面不能是pc两个字符,否则就重复计算了

 

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int L;cin>>L;string a;cin>>a;
        int ans=0;
        for(int i=0;i<L;i++)
        {
            if(a[i]=='C'&&a[i+1]=='C'&&a[i+2]=='P'&&a[i+3]=='C'){
                ans++;
            }
        }
        for(int i=0;i<L;i++)
        {
            int flag=0;
            if(a[i]=='C'&&a[i+1]=='C'&&a[i+2]=='C'){
                if(a[i+3]!='P'||a[i+4]!='C')flag++;
            }
            if(i-1<0&&a[i]=='C'&&a[i+1]=='P'&&a[i+2]=='C'||a[i]=='C'&&a[i+1]=='P'&&a[i+2]=='C'&&a[i-1]!='C'){
                flag=1;
            }
            if(a[i]=='C'&&a[i+1]=='C'&&a[i+2]=='P'&&a[i+3]!='C'){
                flag=1;
            }
            if(flag){
                ans++;break;
            }
        }
        cout<<ans<<endl;

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值