ZOJ - 3985 String of CCPC ʕ •ᴥ•ʔ

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

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.

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

先找到所有CCPC的组合 然后找CCC CCP CPC  注意:CCCPC CCC与CCPC 重复这里我们需要特判

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define N 4000000
#define ll long long 
using namespace std;
//f[n]=f[n-1]*(4*n-2)/(n+1)
#define mod 1000000007
int vis[200010];
string s;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		cin>>s;
		int ans=0; 
		for(int i=0;i<n;i++)
		{
			if(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='P'&&s[i+3]=='C')//找到所有得CCPC 
			ans++;
		}
		for(int i=0;i<n;i++)// 三字符 CPC CCP CCC 特例 CCCPC  CCC与CCPC 重复 
		{
			int flag=0;
			if(s[i]=='C'&&s[i+1]=='P'&&s[i+2]=='C')// 为 
			{
				if(i==0||s[i-1]!='C')
				flag++;
			}
			if(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='P')
			{
				if(s[i+3]!='C')
				flag++;
			}
			if(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='C')
			{
				if(s[i+3]=='P'&&s[i+4]=='C')
				flag--;
				flag++;
			}
			if(flag)
			{
				ans++;
				break;
			} 
		}
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/henucm/article/details/81583987
ZOJ
今日推荐