您的位置:首页 > 其它

bzoj3790 神奇项链

2017-06-18 15:28 155 查看
传送门

既然是找回文串,那么我们就manacher一发,接下来事情就显然多了,就是一道和线段覆盖类似的题,于是我们只要将所有回文区间记录下来,按照先左端点后右端点的优先级将这些区间进行排序,再贪心一遍就好了。

CODE:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int l,r;
inline bool operator <(const node &other)const
{
if(l==other.l) return r>other.r;
return l<other.l;
}
}P[100005];
char s[50005],a[100005];
int p[100005];
int len,Len,pos,mx,ans,wh,tmp;
inline int max(const int &a,const int &b){return a>b?a:b;}
inline int min(const int &a,const int &b){return a<b?a:b;}
int main()
{
while(scanf("%s",s)==1)
{
len=strlen(s);
Len=mx=ans=0;
memset(p,0,sizeof(p));
for(int i=0;i<len;i++)
a[++Len]='#',a[++Len]=s[i];
a[0]='$',a[Len+1]=a[Len+2]='#';
for(int i=1;i<=Len;i++)
{
if(mx>=i) p[i]=min(p[pos*2-i],mx-i);
else p[i]=1;
while(a[i-p[i]]==a[i+p[i]]) p[i]++;
if(i+p[i]>mx) mx=i+p[i],pos=i;
}
for(int i=1;i<=Len;i++)
P[i].l=i-p[i]+1,P[i].r=i+p[i]-1;
sort(P+1,P+Len+1);
wh=P[1].r;tmp=1;pos=0;
while(wh<Len)
{
while(P[tmp].l<=wh) pos=max(pos,P[tmp++].r);
wh=pos;ans++;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  manacher 贪心