2017 CCPC秦皇岛 E题 String of CCPC
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 .
<h4< dd="">Output
For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.
<h4< dd="">Sample Input
- 3
- 3
- CCC
- 5
- CCCCP
- 4
- CPCP
<h4< dd="">Sample Output
- 1
- 1
- 1
<h4< dd="">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,花费是0,1,2,3…如此递增的。每个“CCPC”可视作一分。问可能出现的最高分数是多少。
题解:我们增加一个“P”至多可能增加一个新的“CCPC”。我们增加一个“C”,最多可以增加两个“CCPC”的同时减少一个“CCPC”,也相当于就加了一个。
所以,我们只需要考虑第一次插入操作就可以。
- 注意边界控制。
参考代码:
- #include<bits/stdc++.h>
- #define rep(i,a,n) for(int i=a;i<n;++i)
- #define per(i,a,n) for(int i=n-1;i>=a;--i)
- #define fi first
- #define se second
- #define mp make_pair
- #define pb push_back
- #define np next_permutation
- #define INF 0x3f3f3f3f
- #define EPS 1e-8
- #define mod 1000000007
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef vector<int,int > vi;
- typedef pair<int,int> pii;
- typedef pair<string,string> pss;
- typedef map<string,string> mss;
- typedef map<string,int> msi;
- string s;
- int m,t,n;
- //加P
- //CCCC 稳+1
- //CCC 后两个C不被用就 +1 否则破坏
- //加C
- //PCC 非CCPC
- //CPC 非CCPC
- //CCP 非CCPC
- int main(){
- ios::sync_with_stdio(false );
- cin>>t;
- while(t--){
- cin>>n;
- cin>>s;
- if(n<=){
- if(s=="CCC" || s=="CCP" || s=="CPC") cout<<<<endl;
- else cout<<<<endl;
- continue;
- }
- int flag=,cnt1=;
- rep(i,,n-){
- if(s[i]=='C' && s[i+]=='C' && s[i+]=='P'){
- // cout<<"#1"<<endl;
- if(i+==n){
- flag=;
- break;
- }
- else if(s[i+]=='C'){
- cnt1++;
- continue;
- }
- else {
- flag=;
- continue;
- }
- }
- else if(s[i]=='C' && s[i+]=='P' && s[i+]=='C'){
- // cout<<"#2"<<endl;
- if(i==){
- flag=;
- continue;
- }
- else if(s[i-]=='C') continue;
- else {
- flag=;
- continue;
- }
- }
- //CCCPP
- else if(s[i]=='C' && s[i+]=='C' && s[i+]=='C'){
- // cout<<"#3"<<endl;
- if(i+==n){
- flag=;
- break;
- }
- else if(s[i+]=='C'){
- flag=;
- continue;
- }
- else {
- if(i+==n){
- flag=;
- break;
- }
- else if(s[i+]=='C') continue;
- else {
- flag=;
- continue;
- }
- }
- }
- }
- if(flag) cnt1++;
- cout<<cnt1<<endl;
- }
- }
2017 CCPC秦皇岛 E题 String of CCPC的更多相关文章
- 2017CCPC秦皇岛 E题String of CCPC&&ZOJ3985【模拟】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3985 题意: 给定一个字符串,由c和p组成,可以添加c或者p. 串中出现一 ...
- 2017 CCPC秦皇岛 A题 A Ballon Robot
The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be teams parti ...
- 2017 CCPC秦皇岛 M题 Safest Buildings
PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parach ...
- 2017 CCPC秦皇岛 L题 One Dimensions Dave
BaoBao is trapped in a one-dimensional maze consisting of grids arranged in a row! The grids are nu ...
- 2017 CCPC秦皇岛 G题 Numbers
DreamGrid has a nonnegative integer . He would like to divide into nonnegative integers and minimi ...
- 2017 CCPC秦皇岛 H题 Prime set
Given an array of integers , we say a set is a prime set of the given array, if and is prime. Ba ...
- 2018 CCPC秦皇岛 C题 Crusader Quest
Crusaders Quest is an interesting mobile game. A mysterious witch has brought great darkness to the ...
- 2017 ccpc哈尔滨 A题 Palindrome
2017 ccpc哈尔滨 A题 Palindrome 题意: 给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\) 思路: 很明显这里的回文串长 ...
- 美团点评2017校招笔试真题-算法工程师A
美团点评2017校招笔试真题-算法工程师A 1.下面哪种STL容器的实现和其它三个不一样 A. set B. deque C. multimap D. map 正确答案: B STL的容器可以分为以下 ...
随机推荐
- mongodb定时删除数据(索引删除)
一 简介:本文介绍创建自动删除数据的TTL索引 二 目的 定时删除数据三 创建方法 db.collection.createIndex(keys, options) options: ex ...
- HTTP的请求方式
GET 请求获取 Request-URI 所标识的资源POST 在 Request-URI 所标识的资源后附加新的数据HEAD 请求获取由 Request-URI 所标识的资源的响应消息报头PUT ...
- ACGallery I: Sequence diagram for reading photos:
AC Photo Gallery is an open-source web app, which designed to organize photos/albums. Codes on Githu ...
- 021.掌握Pod-Pod调度策略
一 Pod生命周期管理 1.1 Pod生命周期 Pod在整个生命周期过程中被系统定义了如下各种状态. 状态值 描述 Pending API Server已经创建该Pod,且Pod内还有一个或多个容器的 ...
- Mybatis实现数据的增删改查
Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...
- Python3.7.1学习(五) 将列表中的元素转化为数字并排序
# 本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: # 有一个数字字符的列表: numbers = ['2', '4', '1', '3']print(numbe ...
- SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)
上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...
- ArcGIS API For Javascript :读取 CSV 文件的方法
我们临时会遇到一些测试数据,通常从数据库中以 CSV 格式导出.最简单实用的方法就是使用 ajax 去读取文件,记得引入 jQuery . 例如,在<ArcGIS JS API :新增一个热力图 ...
- boost.asio新框架的设计概念总结
1.66版本,boost.asio库重新设计了框架,目前最新版为1.71.读了几天代码后,对框架中相关概念总结.因为是泛型编程的库,所以分析的概念层的设计. 可通过boost官方文档,strand的1 ...
- Centos下的MySQL安装及配置
里使用的是VMware虚拟机和Centos7系统 虚拟机安装这里不多讲,网上教程很多了,这里就介绍下虚拟机的网络配置. 虚拟机网络配置 Centos网络连接模式这里设置为桥接模式,不用勾选复制物理网络 ...