zoukankan      html  css  js  c++  java
  • 【思维】ABC

    题目描述

    You are given a string s consisting of A, B and C.
    Snuke wants to perform the following operation on s as many times as possible:
    Choose a contiguous substring of s that reads ABC and replace it with BCA.
    Find the maximum possible number of operations.
    Constraints
    1≤|s|≤200000
    Each character of s is A, B and C.

    输入

    Input is given from Standard Input in the following format:
    S

    输出

    Find the maximum possible number of operations.

    样例输入

    ABCABC
    

    样例输出

    3

    提示

    You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.


    参考:SZG大佬的题解

    题意

    在一个只包含A、B、C的字符串,有一种操作,可使 “ABC” 变成 ”BCA“,求字符串s的最多操作数。

    1s200000

    思路

      易得,该操作是将A与BC交换位置,可用 1、0分别代表“A”、“BC”。题意转化对一个只包含10的序列,

    将所有的10更新01,即将所有的0放在1前面。假设序列中共有kk个0,每个0前面有ai个1,则ans=ai (1,k)

      对于单独B、C,则可看作是两个序列分隔的标志。


     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int N = 2e5+100;
     4 typedef long long ll;
     5 char s[N];
     6 int main()
     7 {
     8     scanf("%s",s);
     9     ll cnt = 0 ;
    10     ll ans = 0 , i = 0 ;
    11     int len = strlen(s);
    12     while ( s[i] ) {
    13         if( s[i] == 'A' ){
    14             //printf("#1 %d 
    " ,i);
    15             cnt ++ ;
    16             i++ ;
    17         }else if ( s[i] == 'B' && s[i+1] == 'C' ){
    18             //printf("#2 %d 
    " ,i);
    19             ans = ans + cnt ;
    20             i+=2 ;
    21             if( i >= len ) break ; 
    22         }else if ( s[i] == 'B' || s[i] == 'C' || s[i] == '' ){
    23             cnt = 0 ;
    24             i++ ;
    25         }
    26     }
    27     //ans = ans + cnt ;
    28     printf("%lld
    ",ans);
    29 }
    ABC
  • 相关阅读:
    利用...来字符检測(swift)
    iOS开发中WebP格式的64位支持处理
    Hadoop与分布式开发
    Lua代码解析-写给C和C++开发人员
    logback使用
    【mysql】更新“”空字符串为NULL
    【java】代码优化点
    【java】java获取对象属性类型、属性名称、属性值
    【mybatis】mybatis中放置IN查询拼接sql过长,IN查询进行分批次查询的处理
    【java】java中替换中括号[ ]操作
  • 原文地址:https://www.cnblogs.com/Osea/p/11211217.html
Copyright © 2011-2022 走看看