URAL 题目1517. Freedom of Choice(后缀数组,求公共最长串)

1517. Freedom of Choice

Time limit: 2.0 second

Memory limit: 64 MB

Background

Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom
of speech"
), another freedom - the freedom of choice - came down on them. In the near future, the inhabitants will have to face the first democratic Presidential election in the history of their country.

Outstanding Albanian politicians liberal Mohammed Tahir-ogly and his old rival conservative Ahmed Kasym-bey declared their intention to compete for the high post.

Problem

According to democratic traditions, both candidates entertain with digging dirt upon each other to the cheers of their voters‘ approval. When occasion offers, each candidate makes an election speech,
which is devoted to blaming his opponent for corruption, disrespect for the elders and terrorism affiliation. As a result the speeches of Mohammed and Ahmed have become nearly the same, and now it does not matter for the voters for whom to vote.

The third candidate, a chairman of Albanian socialist party comrade Ktulhu wants to make use of this situation. He has been lazy to write his own election speech, but noticed, that some fragments of
the speeches of Mr. Tahir-ogly and Mr. Kasym-bey are completely identical. Then Mr. Ktulhu decided to take the longest identical fragment and use it as his election speech.

Input

The first line contains the integer number N (1 ≤ N ≤ 100000). The second line contains the speech of Mr. Tahir-ogly.
The third line contains the speech of Mr. Kasym-bey. Each speech consists of N capital latin letters.

Output

You should output the speech of Mr. Ktulhu. If the problem has several solutions, you should output any of them.

Sample

input output
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA

Problem Author: Ilya Grebnov, Nikita Rybak, Dmitry Kovalioff

Problem Source: Timus Top Coders: Third Challenge

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
using namespace std;
char str[400100];
int sa[400100],Rank[400100],rank2[400100],height[400100],c[400100],*x,*y;
int n;
void cmp(int n,int sz)
{
	int i;
	memset(c,0,sizeof(c));
	for(i=0;i<n;i++)
		c[x[y[i]]]++;
	for(i=1;i<sz;i++)
		c[i]+=c[i-1];
	for(i=n-1;i>=0;i--)
		sa[--c[x[y[i]]]]=y[i];
}
void build_sa(char *s,int n,int sz)
{
	x=Rank,y=rank2;
	int i,j;
	for(i=0;i<n;i++)
		x[i]=s[i],y[i]=i;
	cmp(n,sz);
	int len;
	for(len=1;len<n;len<<=1)
	{
		int yid=0;
		for(i=n-len;i<n;i++)
		{
			y[yid++]=i;
		}
		for(i=0;i<n;i++)
			if(sa[i]>=len)
				y[yid++]=sa[i]-len;
			cmp(n,sz);
		swap(x,y);
		x[sa[0]]=yid=0;
		for(i=1;i<n;i++)
		{
			if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])
				x[sa[i]]=yid;
			else
				x[sa[i]]=++yid;
		}
		sz=yid+1;
		if(sz>=n)
			break;
	}
	for(i=0;i<n;i++)
		Rank[i]=x[i];
}
void getHeight(char *s,int n)
{
	int k=0;
	for(int i=0;i<n;i++)
	{
		if(Rank[i]==0)
			continue;
		k=max(0,k-1);
		int j=sa[Rank[i]-1];
		while(s[i+k]==s[j+k])
			k++;
		height[Rank[i]]=k;
	}
}
int ml,mr;
int judge(int len,int k)
{
	int i;
	for(i=1;i<len;i++)
	{
		if(height[i]>=k)
		{
			if(sa[i]>n&&sa[i-1]<=n||sa[i-1]>n&&sa[i]<=n)
			{
				ml=sa[i];
				mr=sa[i]+height[i];
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
//	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i,j;
		scanf("%s",str);
		str[n]=127;
		scanf("%s",str+n+1);
		int len=strlen(str);
		build_sa(str,len,128);
		getHeight(str,len);
		int l=0,r=n;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(judge(len,mid))
			{
				l=mid+1;
			}
			else
				r=mid-1;
		}
		for(j=ml;j<mr;j++)
		{
		//	printf("%d",s[j]);
			printf("%c",str[j]);
		}
		printf("\n");
	}
}

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

时间: 2024-10-18 17:17:08

URAL 题目1517. Freedom of Choice(后缀数组,求公共最长串)的相关文章

URAL 1517 Freedom of Choice(后缀数组,最长公共字串)

题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];}//yuan lai zhi qian ba zhe li de l cuo dang cheng 1 le ... void da(int *r,int *sa,int n,int m) { int i,j

URAL 1297. Palindrome(后缀数组求最大回文串)

题目大意:给你一串字符串,让你求出来它存在的最长连续的回文串. 解题思路:先把字符串逆序加到数组中,然后用后缀数组求解.两种方法:1,枚举排名,直接比较rank相同的字符串的位置差是不是len.如果是的话,就记录求解:2,枚举地址,求第i地址与第2*len-i+1的lcp的最大值. PS:需要注意如果多解输出靠前的字符串. 两种写法写在了一起,分别是Del,和Del1函数. 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB T

UVA 题目760 DNA Sequencing (后缀数组求两个串最长公共子串,字典序输出)

 DNA Sequencing  A DNA molecule consists of two strands that wrap around each other to resemble a twisted ladder whose sides, made of sugar and phosphate molecules, are connected by rungs of nitrogen-containing chemicals called bases. Each strand is

POJ 题目3261 Milk Patterns(后缀数组求最长重叠至少k次的子串长度)

Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12128   Accepted: 5387 Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation,

URAL 1517 Freedom of Choice

Freedom of Choice Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on Ural. Original ID: 151764-bit integer IO format: %lld      Java class name: (Any) Background Before Albanian people could bear with the freedom of speech (this

POJ 2774 Long Long Message &amp;&amp; URAL 1517. Freedom of Choice(求最长重复子序列)

两个题目意思差不多,都是让求最长公共子串,只不过poj那个让输出长度,而URAL那个让输出一个任意的最长的子串. 解体思路: Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 22313   Accepted: 9145 Case Time Limit: 1000MS Description The little cat is majoring in physics in the cap

URAL 1297. Palindrome(后缀数组 求最长回文子串)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1297 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots

POJ 题目3415 Common Substrings(后缀数组+栈,求可以匹配到的长度大于k的公共子串个数)

Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8471   Accepted: 2798 Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given two strings A, B and one integer K, we define S, a

HDOJ 题目4416 Good Article Good sentence(后缀数组求a串子串在b串中不出现的种类数)

-每周六晚的BestCoder(有米!) Good Article Good sentence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2784    Accepted Submission(s): 785 Problem Description In middle school, teachers used to encour