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 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
28 VOTEFORTHEGREATALBANIAFORYOU CHOOSETHEGREATALBANIANFUTURE
Sample Output
THEGREATALBANIA
Source
解题:后缀数组的应用,最长公共子串一定是排序后某相邻两个后缀的lcp。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 200020; 18 int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn]; 19 bool cmp(int *r,int i,int j,int k){ 20 return r[i] == r[j] && r[i+k] == r[j+k]; 21 } 22 void da(int *r,int *sa,int n,int m){ 23 int i,k,p,*x = rk,*y = wb; 24 for(i = 0; i < m; ++i) wd[i] = 0; 25 for(i = 0; i < n; ++i) wd[x[i] = r[i]]++; 26 for(i = 1; i < m; ++i) wd[i] += wd[i-1]; 27 for(i = n-1; i >= 0; --i) sa[--wd[x[i]]] = i; 28 29 for(p = k = 1; p < n; k <<= 1, m = p){ 30 for(p = 0,i = n-k; i < n; ++i) y[p++] = i; 31 for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k; 32 for(i = 0; i < n; ++i) wv[i] = x[y[i]]; 33 for(i = 0; i < m; ++i) wd[i] = 0; 34 for(i = 0; i < n; ++i) wd[wv[i]]++; 35 for(i = 1; i < m; ++i) wd[i] += wd[i-1]; 36 for(i = n-1; i >= 0; --i) sa[--wd[wv[i]]] = y[i]; 37 38 swap(x,y); 39 x[sa[0]] = 0; 40 for(p = i = 1; i < n; ++i) 41 x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++; 42 } 43 } 44 void calcp(int *r,int *sa,int n){ 45 for(int i = 1; i <= n; ++i) rk[sa[i]] = i; 46 int h = 0; 47 for(int i = 0; i < n; ++i){ 48 if(h > 0) h--; 49 for(int j = sa[rk[i]-1]; j+h < n && i+h < n; ++h) 50 if(r[i+h] != r[j+h]) break; 51 lcp[rk[i]-1] = h; 52 } 53 } 54 char sc[maxn],sb[maxn]; 55 int sa[maxn],r[maxn]; 56 int main() { 57 int len; 58 while(~scanf("%d",&len)){ 59 scanf("%s",sc); 60 scanf("%s",sb); 61 sc[len] = '