您的位置:首页 > 其它

String of CCPC(CCPC秦皇岛)

2017-11-05 17:19 417 查看
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.

//思路

暴力即可...(一开始还用了KMP...)
题意可得,最多买1个字母(因为多买肯定亏,买2个最多不亏不赚,不如不买)。
所以,统计CCPC、CPC、CCP出现的次数,若遇到CCC,则要判断他是否是CCCPC,不是的话ans++;
ans为CCPC出现的次数。
如果CPC或CCP出现的次数大于ans,且没出现过CCC但不是CCCPC的情况,ans++即为答案。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 2E5 + 10;

int n;
char str[MAX];

int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
scanf("%s", str);
int len = n;

for (int i = 0; i < 6; i++)
str[len++] = '0';

int ans = 0;
int flag = 0;
int flag1 = 0; //cpc
int flag2 = 0; //ccp

for (int i = 0; i < n; i++)
{
if (str[i] == 'C'&&str[i + 1] == 'C'&&str[i + 2] == 'P'&&str[i + 3] == 'C')
{
ans++;
}
if (str[i] == 'C'&&str[i + 1] == 'C'&&str[i + 2] == 'C'&&flag != 1)
{
if (str[i + 3] == 'C')
{
ans++;
flag = 1;
}
else if (str[i + 3] == 'P'&&str[i + 4] == 'C')
{
ans = ans;
}
else
{
ans++;
flag = 1;
}
}
if (str[i] == 'C'&&str[i + 1] == 'P'&&str[i + 2] == 'C')
{
flag1++;
}
if (str[i] == 'C'&&str[i + 1] == 'C'&&str[i + 2] == 'P')
{
flag2++;
}
}
if (flag != 1)
{
if (flag1 > ans || flag2 > ans)
ans++;
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: