zoukankan      html  css  js  c++  java
  • 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”,也相当于就加了一个。

    所以,我们只需要考虑第一次插入操作就可以。
    - 注意边界控制。
    参考代码:

      1 #include<bits/stdc++.h>
      2 #define rep(i,a,n) for(int i=a;i<n;++i)
      3 #define per(i,a,n) for(int i=n-1;i>=a;--i)
      4 #define fi first
      5 #define se second
      6 #define mp make_pair
      7 #define pb push_back
      8 #define np next_permutation
      9 #define INF 0x3f3f3f3f
     10 #define EPS 1e-8
     11 #define mod 1000000007
     12 using namespace std;
     13 typedef long long ll;
     14 typedef unsigned long long ull;
     15 typedef long double ld;
     16 typedef vector<int,int > vi;
     17 typedef pair<int,int> pii;
     18 typedef pair<string,string> pss;
     19 
     20 typedef map<string,string> mss;
     21 typedef map<string,int> msi;
     22 
     23 string s;
     24 int m,t,n;
     25 //加P
     26 //CCCC 稳+1
     27 //CCC 后两个C不被用就 +1  否则破坏
     28 //加C
     29 //PCC 非CCPC
     30 //CPC 非CCPC
     31 //CCP 非CCPC
     32 
     33 int main(){
     34     ios::sync_with_stdio(false );
     35     cin>>t;
     36     while(t--){
     37         cin>>n;
     38         cin>>s;
     39         if(n<=3){
     40             if(s=="CCC" || s=="CCP" || s=="CPC") cout<<1<<endl;
     41             else cout<<0<<endl;
     42             continue;
     43         }
     44         int flag=0,cnt1=0;
     45         rep(i,0,n-2){
     46             if(s[i]=='C' && s[i+1]=='C' && s[i+2]=='P'){
     47 //                cout<<"#1"<<endl;
     48                 if(i+3==n){
     49                     flag=1;
     50                     break;
     51                 }
     52                 else if(s[i+3]=='C'){
     53                     cnt1++;
     54                     continue;
     55                 }
     56                 else {
     57                     flag=1;
     58                     continue;
     59                 }
     60             }
     61             else if(s[i]=='C' && s[i+1]=='P' && s[i+2]=='C'){
     62 //                cout<<"#2"<<endl;
     63                 if(i==0){
     64                     flag=1;
     65                     continue;
     66                 }
     67                 else if(s[i-1]=='C') continue;
     68                 else {
     69                     flag=1;
     70                     continue;
     71                 }
     72             }
     73             //CCCPP
     74             else if(s[i]=='C' && s[i+1]=='C' && s[i+2]=='C'){
     75 //                cout<<"#3"<<endl;
     76                 if(i+3==n){
     77                     flag=1;
     78                     break;
     79                 }
     80                 else if(s[i+3]=='C'){
     81                     flag=1;
     82                     continue;
     83                 }
     84                 else {
     85 
     86                     if(i+4==n){
     87                         flag=1;
     88                         break;
     89                     }
     90                     else if(s[i+4]=='C') continue;
     91                     else {
     92                         flag=1;
     93                         continue;
     94                     }
     95                 }
     96             }
     97 
     98 
     99 
    100         }
    101         if(flag) cnt1++;
    102         cout<<cnt1<<endl;
    103     }
    104 
    105 }
    View Code

      

  • 相关阅读:
    The Quad
    将OrCAD Capture CIS的设计文件(.dsn)导入到PADS Logic VX.2.3
    OrCAD Capture CIS 16.6 将版本16.6的设计文件另存为版本16.2的设计文件
    Eclipse IDE 添加jar包到Java工程中
    PADS Logic VX.2.3 修改软件界面语言
    切换Allegro PCB Editor
    Allegro PCB Design GXL (legacy) 将brd文件另存为低版本文件
    Allegro PCB Design GXL (legacy) 设置自动保存brd文件
    Could not create an acl object: Role '16'
    windows 下apache开启FastCGI
  • 原文地址:https://www.cnblogs.com/csushl/p/9787694.html
Copyright © 2011-2022 走看看