ZOJ

来源:互联网 发布:小米主题 windows xp 编辑:程序博客网 时间:2024/06/08 00:53

String of CCPC

Time Limit: 1 Second      Memory Limit: 65536 KB

BaoBao has just found a string  of length  consisting of 'C' and 'P' in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring  of  is "good", if and only if  'C', and  'P', where  denotes the -th character in string . The value of  is the number of different "good" substrings in . Two "good" substrings  and  are different, if and only if .

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  units of value.

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

The first line contains an integer  (), indicating the length of string .

The second line contains the string  () consisting of 'C' and 'P'.

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

Output

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

Sample Input

33CCC5CCCCP4CPCP

Sample Output

111

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.


题意: 有一个只有包含C和P两种字符的字符串,问只在该字符串某一个位置插入一个'C'或'P',最多可使该字符串中含有多少个"CCPC",可以两个CCPC共用同一个C,如CCPCCPC算是有两个


因为只能插入一次且只能插入一个,所以要使CCPC个数最大就得使得插入不破坏原来存在的CCPC

就是从三个字符变到四个字符

1、CCC ---- 要将P插入在第二个C之后 且该串后不能是PC ,不然插入后就变成了 CCPCPC,破坏了一个,总数不变

2、CCP ---- 将C插入在P之后且P之后原本的字符不能为C

3、CPC ---- 将C插在最前面,且第一个C之前一个字符不能为C

4、PCC ---- 不能变为CCPC


#include <iostream>#include <string.h>#include <stdio.h>using namespace std;const int N = 2e5 + 10;int T,n;char s[N];bool CCC(int p){    if(p+2>=n) return false;    if(p+4<n&&s[p+3]=='P'&&s[p+4]=='C') return false;    if(s[p]=='C'&&s[p+1]=='C'&&s[p+2]=='C') return true;    return false;}bool CPC(int p){    if(p+2>=n||(p>0&&s[p-1]=='C')) return false;    if(s[p]=='C'&&s[p+1]=='P'&&s[p+2]=='C') return true;    return false;}bool CCP(int p){    if(p+2>=n||(p+2<n-1&&s[p+3]=='C')) return false;    if(s[p]=='C'&&s[p+1]=='C'&&s[p+2]=='P') return true;    return false;}bool CCPC(int p){    if(p+3>=n) return false;    if(s[p]=='C'&&s[p+1]=='C'&&s[p+2]=='P'&&s[p+3]=='C') return true;    return false;}int main(){    scanf("%d",&T);    while(T--){        scanf("%d%s",&n,s);        int ans = 0,add = 0;        for(int i=0;i<n;i++){            if(!add&&(CCC(i)||CCP(i)||CPC(i))) add = 1;            if(CCPC(i)) ans++;        }        printf("%d\n",ans+add);    }    return 0;}




原创粉丝点击