ZOJ - 3985 String of CCPC

本文介绍了一种针对ACM编程竞赛中字符串操作题目的解决策略,通过分析特定字符串模式,如“CCPC”、“CCCPC”等,来最大化字符串的价值。文章提供了具体的样例输入输出及解析,并附带了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String of CCPC

Time Limit: 1 Second Memory Limit: 65536 KB

BaoBao has just found a string SS of length nn consisting of ‘C’ and ‘P’ in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring SiSi+1Si+2Si+3SiSi+1Si+2Si+3 of SS is “good”, if and only if Si=Si+1=Si+3=Si=Si+1=Si+3= ‘C’, and Si+2=Si+2= ‘P’, where SiSi denotes the ii-th character in string . The value of is the number of different “good” substrings in SS. Two “good” substrings SiSi+1Si+3SiSi+1Si+3 andSj=Sj+1=Sj+3Sj=Sj+1=Sj+3 are different, if and only ifi!=ji!=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 . But everything comes with a cost. If it’s the -th time for BaoBao to buy a character, he will have to spend i−1i−1 units of value.

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

The first line contains an integer n(1<=n<=2∗105)n(1<=n<=2∗105), indicating the length of stringSS .

The second line contains the string s (|s| = n) consisting of ‘C’ and ‘P’.

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

Output

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

Sample Input



CCC 

CCCCP 

CPCP

Sample Output



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.

在原先的基础上 看能不能构造出一个 ccpc
找 子串 ”CPC“ "CCC" "CCP"
对于 “CCCPC” “CCC”与“CCPC”互相影响 需要特判

把需要找的子串单独写出来 然后 查找 判断


多买肯定吃亏,
买一个 赚1
买两个 赚1亏1
买三个 亏
所以最多买一个字母 才是赚的哦

 

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
using namespace std;

string s="CCPC",ss="CCCPC",s1="CPC",s2="CCC",s3="CCP";
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int len;
        string str;
        cin>>len>>str;
        int ans=0,flag=0;
        for(int i=0;i<len;i++)
        {
            if(i+5<=len)
            {
                string temp=str.substr(i,5);
                if(temp==ss) // CCCPC
                {
                    ans++;
                    i+=3;
                    continue;
                }
            }
            if(i+4<=len)
            {
                string temp=str.substr(i,4);
                if(temp==s)//CCPC
                {
                    ans++;
                    i+=2;
                }
            }
            if(!flag&&i+3<=len)//买过了就不买了 再买就亏了
            {
                string temp=str.substr(i,3);
                if(temp==s1||temp==s2||temp==s3)
                    flag=1;//需要买
            }
        }
        if(flag) cout<<ans+1<<endl;
        else cout<<ans<<endl;
    }return 0;
}

 

年纪轻轻的玩什么acm啊 活着不好吗? 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值