# 【bzoj3790】神奇项链 Manacher+贪心

abcdcba
abcdef

0
2
5

Manacher+贪心，时间复杂度瓶颈在于排序

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 250010
using namespace std;
struct data
{
int l , r;
}a[N];
char str[N] , tmp[N << 1];
int n , p[N << 1];
bool cmp(data a , data b)
{
return a.l < b.l;
}
int main()
{
while(scanf("%s" , str + 1) != EOF)
{
n = strlen(str + 1);
int i , mx = 0 , last , tot = 0 , pl = 0 , ans = 0;
tmp[0] = '0';
for(i = 1 ; i <= n ; i ++ ) tmp[i * 2 - 1] = '#' , tmp[i * 2] = str[i];
n = n * 2 + 1 , tmp[n] = '#';
for(i = 1 ; i <= n ; i ++ )
{
if(mx >= i) p[i] = min(p[last * 2 - i] , mx - i + 1);
else p[i] = 1;
while(tmp[i - p[i]] == tmp[i + p[i]]) p[i] ++ ;
if(mx < i + p[i] - 1) mx = i + p[i] - 1 , last = i;
a[++tot].l = (i - p[i] + 2) / 2 , a[tot].r = (i + p[i] - 2) / 2;
}
sort(a + 1 , a + tot + 1 , cmp);
n /= 2;
i = 1;
while(pl < n)
{
mx = 0;
while(a[i].l <= pl + 1) mx = max(mx , a[i].r) , i ++ ;
ans ++ , pl = mx;
}
printf("%d\n" , ans - 1);
}
return 0;
}

|转载请注明 [原文链接][作者] ，谢谢！

posted @ 2017-05-03 17:32  GXZlegend  阅读(331)  评论(0编辑  收藏  举报