ZOJ 3985 String of CCPC (水,字符串判断)

版权声明:欢迎加好友讨论~ QQ1336347798 https://blog.csdn.net/henu_xujiu/article/details/81486680

题目链接

                                                                                         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.

就是一个大水题,还卡了我两天,一开始wa是因为只循环一遍,碰到能加的就直接退出了

先遍历找出CCPC。再遍历一次找能加的就可以了

ac


#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <vector>
#define maxn 100005
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		string s;
		cin>>s;
		int ans=0;
		for(int i=0;i<s.size();i++)
		{
			if(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='P'&&s[i+3]=='C')
			{
				ans++;
				i+=2;
				continue;
			}
		}
		
		for(int i=0;i<s.size();i++)
		{
			if(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='C'&&s[i+3]=='P'&&s[i+4]=='C')
			{
				i+=3;
				continue;
			}
			if(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='P'&&s[i+3]=='C')
			{
				i+=2;
				continue;
			}
			if((s[i]=='C'&&s[i+1]=='P'&&s[i+2]=='C')||(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='C')||(s[i]=='C'&&s[i+1]=='C'&&s[i+2]=='P'))
			{
					ans++;
					break;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81486680
今日推荐