标签:store == max class aaa continue for std \n
题意略。
思路:
将字符分桶,然后暴力去扫,扫完合并。假设有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\n",ans); return 0; } /* zaaaabcdaaaaz */
标签:store == max class aaa continue for std \n
原文地址:https://www.cnblogs.com/tiberius/p/9257557.html