【BZOJ 3790】神奇项链

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3790
离线版题目:http://paste.ubuntu.com/17470379/
吐槽:板题,直接上代码不解释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN = 120000;

char pat[MAXN],chr[MAXN];
int n,l[MAXN],r[MAXN],len[MAXN],ord[MAXN];

namespace FenwickTree{
	#define BIT FenwickTree
	#define low(x) (x&(-x))
	#define INF 1000000000
	int arr[MAXN],buf[MAXN];

	inline void init(){
		for (int i=1;i<=n;i++)
			arr[i] = buf[i] = INF;
	}

	inline int query(int left, int right){
		if (right < left) return INF; else { int ret = INF; while (right >= left){
				if (right-low(right)+1>=left) ret = min(ret,arr[right]),right-=low(right);
				else ret = min(ret,buf[right]), right--;
			}
			return ret;
		}
	}

	inline void modify(int pos, int nv){
		if (buf[pos] > nv) {
			buf[pos] = nv;
			for (int i=pos;i<=n;i+=low(i)) if (arr[i] > nv) arr[i] = nv;
				else break;
		} else return;
	}
};

inline bool sort_cmp(const int &A, const int &B){
	return r[A] < r[B];
}

inline int manacher(){
	int m=n*2+1,itr=0;
	for (int i=1;i<=n;i++)
		chr[i*2-1] = '@',
		chr[i*2] = pat[i];
	chr[m] = '@'; chr[m+1] = '#'; chr[0] = '$';

	for (int i=1,p1,p2;i<=m;i++){ if (itr+len[itr] > i)  len[i] = min(len[2*itr-i],itr+len[itr]-i);
		else len[i] = 0;
		p1 = i-len[i]; p2 = i+len[i];
		while (chr[--p1] == chr[++p2]) len[i]++;
		if (itr+len[itr] < i+len[i]) itr = i;
	}

	for (int i=1;i<=m;i++)
		r[i] = i+len[i],
		l[i] = i-len[i],
		ord[i] = i;
	sort(ord+1,ord+m,sort_cmp);

	n = m; BIT::init(); BIT::modify(1,0);
	for (int j=1,i=ord[j],tmp;j<=n;j++,i=ord[j]){
		tmp = BIT::query(l[i],r[i]-1)+1;
		BIT::modify(r[i],tmp);
	}

	return BIT::query(m-1,m)-1;
}

int main(){
	while (~scanf("%s",pat+1)){
		n = strlen(pat+1);
		printf("%dn",manacher());
	}
	return 0;
}

20 thoughts to “【BZOJ 3790】神奇项链”

  1. Thanks for one’s marvelous posting! I definitely enjoyed reading it, you are a great author.

    I will be sure to bookmark your blog and may come back
    very soon. I want to encourage you continue your great posts, have a
    nice day!

  2. First of all I want to say superb blog! I had
    a quick question that I’d like to ask if you don’t mind.
    I was curious to find out how you center yourself and clear your thoughts before writing.
    I have had a hard time clearing my thoughts in getting my thoughts out there.
    I truly do take pleasure in writing however it just seems like the first 10 to 15
    minutes are generally lost simply just trying
    to figure out how to begin. Any recommendations or hints?
    Thanks!

  3. You could certainly see your expertise in the work you write.

    The sector hopes for more passionate writers like you who are not afraid to say how they believe.
    At all times follow your heart.

  4. Simply wish to say your article is as astounding.
    The clarity to your put up is simply cool and that i could assume you’re an expert on this
    subject. Well with your permission let me to snatch your feed to keep up to date with forthcoming post.
    Thank you a million and please carry on the gratifying work.

  5. Do you have a spam problem on this website; I also
    am a blogger, and I was curious about your situation; we have developed some nice methods and we are looking to trade
    strategies with other folks, why not shoot me an email if interested.

  6. I have read a few just right stuff here. Definitely worth bookmarking for revisiting.

    I wonder how a lot effort you put to make such a excellent informative website.

  7. Wonderful article! This is the kind of info that should be shared
    around the net. Shame on Google for now not positioning this publish higher!

    Come on over and discuss with my web site
    . Thanks =)

  8. Great blog here! Additionally your site so much up fast! What web host are you the use of? Can I am getting your associate hyperlink in your host? I wish my site loaded up as quickly as yours lol

Leave a Reply

Your email address will not be published. Required fields are marked *