码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 5371 Hotaru's problem manacher+优先队列+二分

时间:2015-08-12 19:30:43      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:


先用求回文串的Manacher算法,求出以第i个点和第i+1个点为中心的回文串长度,记录到数组c中 比如 10 9 8 8 9 10 10 9 8 我们通过运行Manacher求出第i个点和第i+1个点为中心的回文串长度 0 0 6 0 0 6 0 0 0

两个8为中心,10 9 8 8 9 10是个回文串,长度是6。 两个10为中心,8 9 10 10 9 8是个回文串,长度是6。

要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,比如上边的两个字符串,共享 8 9 10这一部分。 也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样。 因为我们已经记录下来以第i个点和第i+1个点为中心的回文串长度, 那么问题可以转化成,相距x的两个数a[i],a[i+x],满足a[i]/2>=x 并且 a[i+x]/2>=x,要求x尽量大

这可以用一个set维护,一开始集合为空,依次取出a数组中最大的元素,将其下标放入set中,每取出一个元素,再该集合中二分查找比i+a[i]/2小,但最大的元素,更新答案。 然后查找集合中比i-a[i]/2大,但最小的元素,更新答案。

答案就是3*an


Hotaru‘s problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1513    Accepted Submission(s): 555


Problem Description
Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
Let‘s define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.

Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
 

Input
There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases. 

For each test case:

the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence

the second line includes N non-negative integers ,each interger is no larger than 109 , descripting a sequence.
 

Output
Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.

We guarantee that the sum of all answers is less than 800000.
 

Sample Input
1 10 2 3 4 4 3 2 2 3 4 4
 

Sample Output
Case #1: 9
 

Source
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年08月12日 星期三 12时44分58秒
File Name     :HDOJ5371.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

const int maxn=100100;

int n;
int str[maxn],ans[maxn*3];
int p[maxn*3],pos,tot;

void pre() { tot=1;
	memset(ans,0,sizeof(ans));
	ans[0]=-2;
	for(int i=0;i<n;i++)
	{
		ans[tot]=-1; tot++;
		ans[tot]=str[i]; tot++;
	}
	ans[tot]=-1;tot++;
}

void manacher()
{
	pos=-1;
	memset(p,0,sizeof(p));
	int mx=-1,mid=-1;
	int len=tot;
	for(int i=0;i<len;i++)
	{
		int j=-1;
		if(i<mx)
		{
			j=2*mid-i;
			p[i]=min(p[j],mx-i);
		}
		else p[i]=1;

		while(i+p[i]<len&&ans[i+p[i]]==ans[i-p[i]])
			p[i]++;

		if(p[i]+i>mx)
		{
			mx=p[i]+i; mid=i;
		}
	}
}

struct Node
{
	int pos,len;
	Node(){}
	Node(int _pos,int _len):pos(_pos),len(_len){}
	void toString()
	{
		printf("pos: %d len: %d\n",pos,len);
	}
	bool operator<(const Node& node) const
	{
		return len<node.len;
	}
};

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int cas=1,T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",str+i);
		pre();
		manacher();

		priority_queue<Node> q;

		for(int i=0;i<tot;i++)
		{
			if(ans[i]==-1)
			{
				Node node(i/2,p[i]/2);
				q.push(node);
			}
		}

		set<int> st;
		set<int>::iterator it;

		int as=0;
		while(!q.empty())
		{
			Node u=q.top(); q.pop();

			if(st.size()==0) { st.insert(u.pos); continue; }

			int left=u.pos-u.len;
			it=st.lower_bound(left);

			if(*it<left||it==st.end()) it--;
			if(*it>=left)
			{
				as=max(as,u.pos-*it);
			}

			int right=u.pos+u.len;
			it=st.lower_bound(right);

			if(*it>right||it==st.end()) it--;
			if(*it<=right)
			{
				as=max(as,*it-u.pos);
			}

			st.insert(u.pos);
		}

		printf("Case #%d: %d\n",cas++,as*3);
	}
    
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 5371 Hotaru's problem manacher+优先队列+二分

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/47448217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!