String of CCPC

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.

题目的意思就是给你一个只含“C”和P的字符串,可以在其中加一个C或P仅有一次机会,问你最多几个“CCPC”串。(CCPCCPC)算两个

我们先考虑,我们新加的C||P不能影响到已经存在的CCPC,否则我们加的就没意义了。

所以我们找到CCPC串后就可以直接跳过,不用再考虑加字符了。

我们去掉CCPC中任意一个可以得到 “CCP”,“CPC”,“CCC”。我们只要对‘CCC’这种情况特殊分析,因为这个子串的情况可能是“CCCPC”如果我们插入字符就会导致下个CCPC串被打乱,失去意义。

下面我要吐槽ZOJ的判题机,我用了sbustr就能过,为什么一个个字符看就会超时!!!

我至今还没想通,也请大佬帮我看看。

TLE代码!!!!!!

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
//999999
int main()
{

    int j,i,n;
    scanf("%d",&n);
    while(n--)
    {
        char str[1000005];
        int len;
        scanf("%d",&len);
        memset(str,0,sizeof(str));
        int ans=0;
        int flag=0;
        scanf("%s",str);
        for(i=0; i<len; i++)
        {

            if(str[i]=='C'&&str[i+1]=='C'&&str[i+2]=='P'&&str[i+3]=='C')
            {

                i=i+2;
                ans++;
                continue;
            }
            if(!flag)
            {
                if(str[i]=='C'&&str[i+1]=='C'&&str[i+2]=='C')
                {
                    if(str[i+3]!='P'&&str[i+4]!='C')
                        flag=1;
                }
                if(str[i]=='C'&&str[i+1]=='C'&&str[i+2]=='P')
                    flag=1;
                if(str[i]=='C'&&str[i+1]=='P'&&str[i+2]=='C')
                    flag=1;

            }

        }
        printf("%d\n",ans+flag);
    }
    return 0;
}

AC代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
//999999
char str[1000005];
int main()
{

    int j,i,n;
    scanf("%d",&n);
    while(n--)
    {
        string str;
        int len;
        scanf("%d",&len);
        str="";
        int ans=0;
        int flag=0;
        cin >>str;
        for(i=0; i<len; i++)
        {
            if(str.substr(i,4)=="CCPC"){
                i+=2;
                ans++;
                continue;
            }
            if(!flag){
                string k=str.substr(i,3);
                if(k=="CCC"||k=="CCP"||k=="CPC"){
                    if(k=="CCC"&&str.substr(i+1,4)=="CCPC") continue;
                    flag=1;
                    //ans++;
                }
            }


        }
        printf("%d\n",ans+flag);
        }
    return 0;
}
//acacacac

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值