CodeForces 909D

Posted tiberius

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 909D相关的知识,希望对你有一定的参考价值。

题意略。

思路:

将字符分桶,然后暴力去扫,扫完合并。假设有k个桶,每个桶里有n / k个数,那么我们应该要扫 n / (2 * k)次,每次的复杂度是k,最后算得复杂度是O(n)。

详见代码:

#include<bits/stdc++.h>
#define maxn 1000005
using namespace std;

struct node{
    int color;
    int numb;
    node(int a = 0,int b = 0){
        color = a,numb = b;
    }
};

node store[maxn];
int tail;
char str[maxn];

int main(){
    scanf("%s",str);
    store[tail++] = node(str[0] - a,1);
    for(int i = 1;str[i];++i){
        if(store[tail - 1].color == str[i] - a) store[tail - 1].numb += 1;
        else store[tail++] = node(str[i] - a,1);
    }
    int ans = 0;
    while(tail > 1){
        for(int i = 0;i < tail;++i){
            if(i == 0 || i == tail - 1) store[i].numb -= 1;
            else store[i].numb -= 2;
        }
        ++ans;
        int temp = tail;
        tail = 0;
        for(int i = 0;i < temp;++i){
            if(store[i].numb <= 0) continue;
            if(tail == 0 || store[i].color != store[tail - 1].color) store[tail++] = store[i];
            else store[tail - 1].numb += store[i].numb;
        }
    }
    printf("%d
",ans);
    return 0;
}

/*
zaaaabcdaaaaz
*/

 

以上是关于CodeForces 909D的主要内容,如果未能解决你的问题,请参考以下文章

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

c_cpp Codeforces片段

Codeforces 86C Genetic engineering(AC自动机+DP)

CodeForces 1005D Polycarp and Div 3(思维贪心dp)

(Incomplete) Codeforces 394 (Div 2 only)

CodeForces 931F Teodor is not a liar!