ural1517之最长公共子串

原创 2013年12月03日 20:46:03


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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=2*100000+10;
int *rank,r[MAX],sa[MAX],height[MAX];
int wa[MAX],wb[MAX],wm[MAX];
char s[MAX];

bool cmp(int *r,int a,int b,int l){
	return r[a] == r[b] && r[a+l] == r[b+l];
}

void makesa(int *r,int *sa,int n,int m){
	int *x=wa,*y=wb,*t;
	for(int i=0;i<m;++i)wm[i]=0;
	for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
	for(int i=1;i<m;++i)wm[i]+=wm[i-1];
	for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
	for(int i=0,j=1,p=0;p<n;j=j*2,m=p){
		for(p=0,i=n-j;i<n;++i)y[p++]=i;
		for(i=0;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
		for(i=0;i<m;++i)wm[i]=0;
		for(i=0;i<n;++i)wm[x[y[i]]]++;
		for(i=1;i<m;++i)wm[i]+=wm[i-1];
		for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
		for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i){
			x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
		}
	}
	rank=x;
}

void calheight(int *r,int *sa,int n){
	for(int i=0,j=0,k=0;i<n;height[rank[i++]]=k){
		for(k?--k:0,j=sa[rank[i]-1];r[i+k] == r[j+k];++k);
	}
}

int main(){
	int n;
	while(cin>>n){
		cin>>s>>s+n+1;
		s[n]='#';
		int len=n;
		for(n=0;s[n] != '\0';++n)r[n]=s[n];
		r[n]=0;
		makesa(r,sa,n+1,256);
		calheight(r,sa,n);
		int sum=0,x=0,y=0;
		for(int i=1;i<=n;++i){
			if(height[i]>sum){
				if(sa[i]<len && sa[i-1]>len)sum=height[i],x=sa[i],y=x+sum;
				if(sa[i]>len && sa[i-1]<len)sum=height[i],x=sa[i-1],y=x+sum;
			}
		}
		for(int i=x;i<y;++i)cout<<s[i];
		cout<<endl;
	}
	return 0;
}


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

相关文章推荐

HDU 1853 Cyclic Tour(二分图最优匹配) 解题报告

转载请注明出自cxb:http://write.blog.csdn.net/postlist 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 ...

HDU-2255 奔小康赚大钱(完备匹配下的最大权匹配) 解题报告

转载请注明出自cxb:http://write.blog.csdn.net/postlist 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 ...

pku 2774 , ural 1517( 后缀数组 最长公共子串 )

题意 : 求两个字符串的最大公共子串 思路 : 讲

Ural1517 Freedom of choice, 后缀数组,最长公共子串

也是看后缀数组中的套题。。。 /*******************************************************************************...
  • neofung
  • neofung
  • 2012年06月05日 21:37
  • 487

POJ 2774 & URAL 1517(最长公共子串)

题目链接:URAL-1517 POJ-2774题目大意:就是给两个字符串,求最长公共子串。解题思路:法一:常规动态规划好像是可以的(虽然我自己没试(~ ̄▽ ̄)~) 因为常规的LCS是O(n^2)的...

URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)

题意:给出两个串的长度(一样长),输出们的

算法学习之最长公共子串

最长公共字串,最长公共子序列,最长递增子序列都是典型的动态规划问题,最长公共子串和最长公共子序列的差别是最长公共子序列可以不连续,但是最长公共子串必须连续。先来看最长公共子串,首先会想到暴力法解决,最...

常见算法问题之最长公共子串问题(Longest common substring problem)

对于寻找两个字符串的最长公共子字符串的问题,暴力搜索的方式的时间复杂度将高达O(n^3), 而通过后缀树的方式可将时间复杂度降低到O(n^2)。以下是我实现的C++源码:#include #incl...
  • jaye16
  • jaye16
  • 2017年02月16日 23:09
  • 251

动态规划之最长公共子串

子字符串的定义和子序列的定义类似,但要求是连续分布在其他字符串中。比如输入两个字符串BDCABA和ABCBDAB的最长公共字符串有BD和AB,它们的长度都是2。 Longest Common Su...

动态规划之最长公共子串问题

最长公共子串问题:一个给定序列的子序列是在该序列中删去若干元素后得到的序列。给定两个序列X和Y,当另一序列Z既是X的子序列又是Y的子序列时,称Z是序列X和Y的公共子序列。最长公共子串就是求给定两个序列...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ural1517之最长公共子串
举报原因:
原因补充:

(最多只允许输入30个字)