Ural1517freedom of Choice (suffix array)

Source: Internet
Author: User

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 the 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 is 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 NCapital Latin Letters.

Output

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

Example

input Output
28VOTEFORTHEGREATALBANIAFORYOUCHOOSETHEGREATALBANIANFUTURE
Thegreatal
Test instructions

Find the longest common subsequence and output.

Inspiration:

This problem has only one longest common subsequence. If there are multiple times and require minimal dictionary order, you can sort the dictionary tree to find the longest common subsequence of the dictionary sequence.

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=4100000;CharSTR1[MAXN],STR2[MAXN]; intL,L1,L2,CH[MAXN];structsa{intCNTA[MAXN],CNTB[MAXN],A[MAXN],B[MAXN]; intRANK[MAXN],SA[MAXN],TSA[MAXN],HT[MAXN]; voidsort () { for(inti =0; I <= -; i + +) cnta[i] =0;  for(inti =1; I <= L; i + +) Cnta[ch[i]] + +;  for(inti =1; I <= -; i + +) Cnta[i] + = cnta[i-1];  for(inti = L; I I--) sa[cnta[ch[i]]---] =i; rank[sa[1]] =1;  for(inti =2; I <= L; i + +) {Rank[sa[i]]= Rank[sa[i-1]]; if(Ch[sa[i]]! = ch[sa[i-1]]) Rank[sa[i]] + +; }          for(intL =1; Rank[sa[l]] < L; L <<=1){               for(inti =0; I <= L; i + +) cnta[i] =0;  for(inti =0; I <= L; i + +) cntb[i] =0;  for(inti =1; I <= L; i + +) {Cnta[a[i]= Rank[i]] + +; Cntb[b[i]= (i + L <= L)? Rank[i + L]:0] ++; }               for(inti =1; I <= L; i + +) Cntb[i] + = cntb[i-1];  for(inti = L; I I--) tsa[cntb[b[i]]---] =i;  for(inti =1; I <= L; i + +) Cnta[i] + = cnta[i-1];  for(inti = L; I I--) sa[cnta[a[tsa[i] []---] =Tsa[i]; rank[sa[1]] =1;  for(inti =2; I <= L; i + +) {Rank[sa[i]]= Rank[sa[i-1]]; if(A[sa[i]]! = a[sa[i-1]] || B[sa[i]]! = b[sa[i-1]]) Rank[sa[i]] + +; }         }    }    voidgetht () { for(inti =1, j =0; I <= L; i + +){              if(j) J--;  while(Ch[i + j] = = Ch[sa[rank[i]-1] + j]) J + +; Ht[rank[i]]=J; }}};sa SA;voidinit () {scanf ("%d",&L1); scanf ("%s", str1+1); scanf ("%s", str2+1); L1=strlen (str1+1); L2=strlen (str2+1);  for(intI=1; i<=l1;i++) ch[i]=str1[i]-'A'+1; CH[L1+1]= -;  for(intI=1; i<=l2;i++) ch[i+l1+1]=str2[i]-'A'+1; L=l1+l2+1; }intMain () {init ();    Sa.sort ();    SA.GETHT (); intans=0, pos=0;  for(inti =1; I <= L; i++){      if(SA.SA[I]&LT;=L1)! = (sa.sa[i-1]<=L1)) if(sa.ht[i]>ans) {ans=sa.ht[i];p os=Sa.sa[i]; }    }     for(inti=pos;i<=pos+ans-1; i++) printf ("%c", ch[i]+'A'-1); return 0;}

Ural1517freedom of Choice (suffix array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.