zoukankan      html  css  js  c++  java
  • 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
    */
  • 相关阅读:
    从1到n中找到任意num个数的和为sum的所有组合
    算法导论5.12
    使用c++技术实现下载网页
    算法导论5.13
    感慨
    算法导论2.37
    [转载]Yahoo!的分布式数据平台PNUTS简介及感悟
    Bigtable 论文笔记
    GFS 论文笔记
    MapReduce论文笔记
  • 原文地址:https://www.cnblogs.com/tiberius/p/9257557.html
Copyright © 2011-2022 走看看