1517. Freedom of Choicetime limit:2.0 second
Memory limit:64 MB
Backgroundbefore Albanian people could bear and the freedom of speech (this story was fully described in the problem "Freedom of Speech"), another freedom-the Freedom of Choice-came on them. In the inhabitants, the Democratic presidential election in the history of their C Ountry. Outstanding Albanian politicians Liberal Mohammed tahir-ogly and his old rival conservative Ahmed Kasym-bey declared their Intention to compete for the high post. Problemaccording to democratic traditions, both candidates entertain with digging dirt upon each other to the cheers of th EIR voters ' approval. When occasion offers, each candidate makes a election speech, which is devoted to blaming he opponent for corruption, Di Srespect for the Elders and terrorism affiliation. As a result the speeches of Mohammed and Ahmed has 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 had 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. Inputthe 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. Outputyou should output the speech of Mr. Ktulhu. If the problem has several solutions, you should output any of them. Sample
input |
Output |
28VOTEFORTHEGREATALBANIAFORYOUCHOOSETHEGREATALBANIANFUTURE |
Thegreatalbania |
problem Author: Ilya Grebnov, Nikita Rybak, Dmitry Kovalioff
problem Source: Timus Top Coders:third Challenge
AC Code
#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;elsex[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;} Elser=mid-1;} for (j=ml;j<mr;j++) {//printf ("%d", S[j]);p rintf ("%c", Str[j]);} printf ("\ n");}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
URAL Topic 1517. Freedom of Choice (suffix array, common longest string)