2017 CCPC 秦皇岛 & ZOJ 3985 - String of CCPC (子串)
2017-12-19 23:23
447 查看
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.
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 .
"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.
Author: WENG, Caizhi
Source: The 2017 China Collegiate Programming Contest, Qinhuangdao Site
题意:
可以加字符。字符有代价 0 1 2 3.
POINT:
容易得知只能加一次。那么直接遍历加的位置,和加的p还是c。
然后往前走5个长度,往后从5个长度。比较这10或11个长度里有几个ccpc,增加了说明就成功了。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 2e5+10;
char s[5]="CCPC";
char str[maxn];
char a[22];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
scanf("%s",str);
int tmp=0;
for(int i=0;i<=n;i++)
{
int cs=0;
int ans1=0,ans2=0,ans3=0;
for(int k=max((i-5),0);k<min(n,i+5);k++)
{
a[cs++]=str[k];
}
for(int j=0;j<cs-3;j++)
{
bool flag=1;
for(int k=0;k<4;k++)
{
if(a[j+k]!=s[k])
flag=0;
}
ans1+=(int)flag;
}
cs=0;
for(int k=max(i-5,0);k<i;k++)
{
a[cs++]=str[k];
}
a[cs++]='P';
for(int k=i;k<min(n,i+5);k++)
{
a[cs++]=str[k];
}
for(int j=0;j<cs-3;j++)
{
bool flag=1;
for(int k=0;k<4;k++)
{
if(a[j+k]!=s[k])
flag=0;
}
ans2+=(int)flag;
}
cs=0;
for(int k=max(i-5,0);k<i;k++)
{
a[cs++]=str[k];
}
a[cs++]='C';
for(int k=i;k<min(n,i+5);k++)
{
a[cs++]=str[k];
}
for(int j=0;j<cs-3;j++)
{
bool flag=1;
for(int k=0;k<4;k++)
{
if(a[j+k]!=s[k])
flag=0;
}
ans3+=(int)flag;
}
if((ans2>ans1)||(ans3>ans1))
{
tmp=1;
break;
}
}
int ans=0;
for(int i=0;i<n-3;i++)
{
int flag=1;
for(int j=0;j<4;j++)
{
if(str[i+j]!=s[j])
flag=0;
}
ans+=flag;
}
printf("%d\n",ans+tmp);
}
return 0;
}
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.
Author: WENG, Caizhi
Source: The 2017 China Collegiate Programming Contest, Qinhuangdao Site
题意:
可以加字符。字符有代价 0 1 2 3.
POINT:
容易得知只能加一次。那么直接遍历加的位置,和加的p还是c。
然后往前走5个长度,往后从5个长度。比较这10或11个长度里有几个ccpc,增加了说明就成功了。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 2e5+10;
char s[5]="CCPC";
char str[maxn];
char a[22];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
scanf("%s",str);
int tmp=0;
for(int i=0;i<=n;i++)
{
int cs=0;
int ans1=0,ans2=0,ans3=0;
for(int k=max((i-5),0);k<min(n,i+5);k++)
{
a[cs++]=str[k];
}
for(int j=0;j<cs-3;j++)
{
bool flag=1;
for(int k=0;k<4;k++)
{
if(a[j+k]!=s[k])
flag=0;
}
ans1+=(int)flag;
}
cs=0;
for(int k=max(i-5,0);k<i;k++)
{
a[cs++]=str[k];
}
a[cs++]='P';
for(int k=i;k<min(n,i+5);k++)
{
a[cs++]=str[k];
}
for(int j=0;j<cs-3;j++)
{
bool flag=1;
for(int k=0;k<4;k++)
{
if(a[j+k]!=s[k])
flag=0;
}
ans2+=(int)flag;
}
cs=0;
for(int k=max(i-5,0);k<i;k++)
{
a[cs++]=str[k];
}
a[cs++]='C';
for(int k=i;k<min(n,i+5);k++)
{
a[cs++]=str[k];
}
for(int j=0;j<cs-3;j++)
{
bool flag=1;
for(int k=0;k<4;k++)
{
if(a[j+k]!=s[k])
flag=0;
}
ans3+=(int)flag;
}
if((ans2>ans1)||(ans3>ans1))
{
tmp=1;
break;
}
}
int ans=0;
for(int i=0;i<n-3;i++)
{
int flag=1;
for(int j=0;j<4;j++)
{
if(str[i+j]!=s[j])
flag=0;
}
ans+=flag;
}
printf("%d\n",ans+tmp);
}
return 0;
}
相关文章推荐
- ZOJ 3985 String of CCPC 2017秦皇岛CCPC(子串个数)
- ZOJ 3985 && 2017CCPC秦皇岛 E:String of CCPC
- 2017 CCPC 秦皇岛 & ZOJ 3981 - Balloon Robot 规律
- 2017 CCPC 秦皇岛 & ZOJ 3983 - Crusaders Quest
- 2017 CCPC 秦皇岛 & ZOJ 3987 - Numbers (贪心+大数)
- 2017 CCPC 秦皇岛 & ZOJ 3993 - Safest Buildings (概率+规律)
- zoj&CCPC秦皇岛站E-思维-String of CCPC
- ZOJ - 3985 String of CCPC (2017CCPC秦皇岛站 简单题)
- 2017CCPC秦皇岛 E题String of CCPC&&ZOJ3985【模拟】
- 2017 CCPC 秦皇岛 E - String of CCPC【规律】
- 2017 CCPC 秦皇岛 M - Safest Buildings 【几何&概率】
- 2017 CCPC 秦皇岛站 M题题解 ZOJ 3993 Safest Buildings
- 2017 CCPC-WFinal&&HDOJ6025 Coprime Sequence(前缀后缀GCD问题)
- 2017 CCPC 秦皇岛 A:Balloon Robot
- ZOJ 3992 && 2017CCPC秦皇岛 L:One-Dimensional Maze
- ZOJ 3981 && 2017CCPC秦皇岛 A:Balloon Robot
- 2017 CCPC秦皇岛 B
- 2017 ccpc 秦皇岛C Crusaders Quest
- ZOJ 3987 && 2017CCPC秦皇岛 G:Numbers(高精度+贪心)
- 2017 ccpc 秦皇岛E String of CCPC