2017秦皇岛 - ICPC - E - String of CCPC 【string的一些简单运用,思维】

String of CCPC

Time Limit: 1 Second Memory Limit: 65536 KB

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 SiSi+1Si+2Si+3 of S is “good”, if and only if Si=Si+1=Si+3= ‘C’, and Si+2= ‘P’, where Si denotes the i -th character in string . The value of is the number of different “good” substrings in S. Two “good” substrings SiSi+1Si+3 and Sj=Sj+1=Sj+3 are different, if and only if i!=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 i1 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<=n<=2105) , 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 over all test cases will not exceed 106.

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.

Author:

WENG, Caizhi

Source:

The 2017 China Collegiate Programming Contest, Qinhuangdao Site

题意: 给你一个长度为n的字符串,你可以在原有字符串的基础上添加任意多个’C’或’P’,但是每次添加一个就要花费一个精力(第一个不算,就是如果你要添加两个的话只花费一个,三个的话花费两个)(这里随便叫的),在任意的地方只要组成”CCPC”就可以获得一个精力,问你到最后可以获得最大的精力是多少。

分析: 刚看到这个题,忽略了原先字符串的顺序,还以为是可以随便排序呢,wa了一发后,读了便题发现,题目读错了,不难想到我们最多就是在原先的基础上再多生成一个”CCPC”,因为即使我们还可以生成的话也要花费一个精力,我们只需要先贪心的把”CCPC”给“挑”出来,然后在判断是否可以利用其它的可以生成目标字符串”CCPC”,这里我们利用string里的substr来截取部分字符串,我们发现下面的部分串添加一个字母后可以组成目标串:”CPC”,”CCC”,”CCP”,这时我们考虑相互的影响,我们发现当原串为”CCCPC”时,我们就会出错,因为我们在”CCC” 和 “CCPC”重复了,这里需要特判下,这里注意后就没有什么注意的了,我们只需判断原串里是否存在:”CPC”,”CCC”,”CCP”,是的话答案上加1即可

坑点 : 这里注意的是当指针找到”CCPC”或”CCCPC”时注意指针需要往后移动

参考代码

#include<bits/stdc++.h>

using namespace std;

string ss = "CCCPC",sss = "CCPC";
string s1 = "CPC",s2 = "CCP",s3 = "CCC";

int main(){
    ios_base::sync_with_stdio(0);
    int T;cin>>T;
    while(T--) {
        int len;cin>>len;
        string s;cin>>s;
        int ans = 0;
        int c = 0,p = 0;
        bool flg = false;
        for(int i = 0;i < len;i++) {
            if(i+5 <= len){
                string t = s.substr(i,5);
                if(t == ss){
                    ans++;
                    i+=3;
                    continue;
                }
            }
            if(i + 4 <= len) {
                string t = s.substr(i,4);
                if(t == sss) ans++,i+=2;
            }
            if(!flg && i+3 <= len) {
                string t = s.substr(i,3);
                if(t == s1 ||  t == s2 || t == s3) {
                    flg = true;
                }
            }
        }
        if(flg) cout<<ans+1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}
  • 如有错误或遗漏,请私聊下UP,thx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值