Ural 1517 freedom of choice

Source: Internet
Author: User
Freedom of choicetime limit: 2000 msmemory limit: 32768 kbthis problem will be judged on Ural. Original ID: 1517
64-bit integer Io format: % LLD Java class name: (any) backgroundbefore Albanian people coshould 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 Hammed Tahir-ougly 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 their voters 'approval. when occasion offers, each candidate makes an election speech, which is already Ted to blaming his opponent for uption, 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. inputthe first line contains the integer number N(1 ≤ N≤ 100000). The second line contains the speech of mr. Tahir-ougly. The third line contains the speech of mr. Kasym-bey. Each speech consists NCapital Latin letters. outputyou shocould output the speech of mr. ktulhu. If the problem has several solutions, you shocould output any of them. sample input
28VOTEFORTHEGREATALBANIAFORYOUCHOOSETHEGREATALBANIANFUTURE
Sample output
THEGREATALBANIA

Sourcetimus top coders: third challenge problem solving: application of the suffix array, the longest common substring must be an LCP with two adjacent suffixes after sorting.
 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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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] = ‘\0‘;62         strcpy(sc+len+1,sb);63         for(int i = 0; i < (len<<1|1); ++i)64              r[i] = sc[i];65         r[len<<1|1] = 0;66         da(r,sa,(len<<1)+2,128);67         calcp(r,sa,len<<1|1);68         int ans = 0,index = 0;69         for(int i = 1; i < (len<<1|1); ++i){70             if(sa[i] < len != sa[i+1] < len){71                 if(lcp[i] > ans){72                     ans = lcp[i];73                     index = sa[i];74                 }75             }76         }77         for(int i = 0; i < ans; ++i)78             putchar(sc[index+i]);79         putchar(‘\n‘);80     }81     return 0;82 }
View code

 

Ural 1517 freedom of choice

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.