​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;
}

 

ZOJ - 3985  String of CCPC_ios

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