Freedom of Choice

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on Ural. Original ID: 1517
64-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。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn];
bool cmp(int *r,int i,int j,int k){
return r[i] == r[j] && r[i+k] == r[j+k];
}
void da(int *r,int *sa,int n,int m){
int i,k,p,*x = rk,*y = wb;
for(i = ; i < m; ++i) wd[i] = ;
for(i = ; i < n; ++i) wd[x[i] = r[i]]++;
for(i = ; i < m; ++i) wd[i] += wd[i-];
for(i = n-; i >= ; --i) sa[--wd[x[i]]] = i; for(p = k = ; p < n; k <<= , m = p){
for(p = ,i = n-k; i < n; ++i) y[p++] = i;
for(i = ; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < n; ++i) wv[i] = x[y[i]];
for(i = ; i < m; ++i) wd[i] = ;
for(i = ; i < n; ++i) wd[wv[i]]++;
for(i = ; i < m; ++i) wd[i] += wd[i-];
for(i = n-; i >= ; --i) sa[--wd[wv[i]]] = y[i]; swap(x,y);
x[sa[]] = ;
for(p = i = ; i < n; ++i)
x[sa[i]] = cmp(y,sa[i-],sa[i],k)?p-:p++;
}
}
void calcp(int *r,int *sa,int n){
for(int i = ; i <= n; ++i) rk[sa[i]] = i;
int h = ;
for(int i = ; i < n; ++i){
if(h > ) h--;
for(int j = sa[rk[i]-]; j+h < n && i+h < n; ++h)
if(r[i+h] != r[j+h]) break;
lcp[rk[i]-] = h;
}
}
char sc[maxn],sb[maxn];
int sa[maxn],r[maxn];
int main() {
int len;
while(~scanf("%d",&len)){
scanf("%s",sc);
scanf("%s",sb);
sc[len] = '\0';
strcpy(sc+len+,sb);
for(int i = ; i < (len<<|); ++i)
r[i] = sc[i];
r[len<<|] = ;
da(r,sa,(len<<)+,);
calcp(r,sa,len<<|);
int ans = ,index = ;
for(int i = ; i < (len<<|); ++i){
if(sa[i] < len != sa[i+] < len){
if(lcp[i] > ans){
ans = lcp[i];
index = sa[i];
}
}
}
for(int i = ; i < ans; ++i)
putchar(sc[index+i]);
putchar('\n');
}
return ;
}

最新文章

  1. APDU
  2. 使用webpack搭建vue开发环境
  3. 修改安全策略组 -- windows
  4. BZOJ 2301 Problem b
  5. webstorm 自定义代码模板
  6. Android(java)学习笔记192:SQLite数据库(表)的创建 以及 SQLite数据库的升级
  7. nexus4/5/6/7/9/10设备谷歌安卓5.1.1系统底包下载
  8. ASP.Net连接SQLServer 连接字符串
  9. Python开发环境Wing IDE使用教程:部分调试功能介绍
  10. history对象 back() forward() go() 和pushState() replaceState()
  11. 京东iPad新品开售销量环比增22倍
  12. hbase-default.xml(Hbase 默认参数翻译)
  13. 如何让PHP程序自动执行(后台)
  14. oldboy s21day05
  15. Mysql 5.7.21 设置主从库同步
  16. Linux记录-自动删除几天前的日志文件
  17. asp.net core 2.0 api ajax跨域问题
  18. IDEA中配置Maven+spring MVC+tomcat
  19. java后端导出excel表格
  20. 一起做RGB-D SLAM (2)

热门文章

  1. php 文件夹 与 文件目录操作
  2. 洛谷—— P1803 凌乱的yyy
  3. [ASP.NET]EF选一个空表的情况
  4. byte类型取值范围以及溢出具体解释
  5. [ES6] Proxy &amp; Reflect
  6. Java向上转型和向下转型(附具体样例)
  7. Ubuntu18.04修改Hostname
  8. BZOJ 1818 线段树+扫描线
  9. linux下通用Makefile写法
  10. line-height与间距总总