String of CCPC(神奇的字符串)

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

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 i s 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 si+2='P',  where si denotes the i-th character in string s. The value of s is the number of different "good" substrings in s. Two "good" substrings  sisi+1si+2si+3 and sjsj+1sj+2sj+3 are different ,if and only ≠ 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.

输入描述:

 
 
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 ≤ n ≤ 2 ×105) indicating the length of strings.
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 106.

输出描述:

For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.
示例1

输入

复制3 3 CCC 5 CCCCP 4 CPCP
3
3
CCC
5
CCCCP
4
CPCP

输出

复制1 1 1
1
1
1

说明

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

//先找CCPC的个数->找CCP、CPC、CCC的个数并且避开CCPC,我们容易发现每次答案最多加1。 验证一下:CCPCPC以及CCPCPCCPCPC

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int N=2e5+10;
string ccpc="CCPC",cpc="CPC",ccp="CCP",ccc="CCC";
string s;
int m,n;
int main(){
    int i;
    cin>>n;
    while(n--)
    {
        cin>>m>>s;
        int cnt,flag;
        flag=cnt=0;
        for(i=0;i<s.length();i++)
        {
            if(s.substr(i,4)==ccpc) //用substr截取四个字符,只要找到ccpc就个数cnt++;
            {
                cnt++;
                i+=2;
                continue;
            }
            if(flag) continue;//只要找到ccp||ccc||cpcj就可以不找了
            string str=s.substr(i,3);
            if(str==cpc||str==ccp||str==ccc)
            {
                if(str==ccc&&s.substr(i+1,4)==ccpc) continue;//因为ccc后面会有ccpc的情况,所以/要判断一下
                cnt++;
                flag=1;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值