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.

Example input

28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE

Example output

THEGREATALBANIA
给你两个等长的串,求他们的最长公共连续子串.

这里要求我们输出这个最长公共串了.(题目中要求有多解就输出所有解,但是其实只有1个解.)

对于输出解,我们只要记录过程中的每一个可以作为解的后缀编号以及长度即可一一输出.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int maxn=200000+1000;
int len1,len2;//分别为串1和串2的长度
int ans,pos;//ans是公共字串的长度,pos是起始位置
struct SuffixArray
{char s[maxn];int sa[maxn],rank[maxn],height[maxn];int t1[maxn],t2[maxn],c[maxn],n;void build_sa(int m){int i,*x=t1,*y=t2;for(i=0; i<m; i++)c[i]=0;for(i=0; i<n; i++)c[x[i]=s[i]]++;for(i=1; i<m; i++)c[i]+=c[i-1];for(i=n-1; i>=0; i--)sa[--c[x[i]]]=i;for(int k=1; k<=n; k<<=1){int p=0;for(i=n-k; i<n; i++)y[p++]=i;for(i=0; i<n; i++)if(sa[i]>=k)y[p++]=sa[i]-k;for(i=0; i<m; i++)c[i]=0;for(i=0; i<n; i++)c[x[y[i]]]++;for(i=1; i<m; i++)c[i]+=c[i-1];for(i=n-1; i>=0; i--)sa[--c[x[y[i]]]]=y[i];swap(x,y);p=1;x[sa[0]]=0;for(i=1; i<n; i++)x[sa[i]]= y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;if(p>=n)break;m=p;}}void build_height(){int i,j,k=0;for(i=0; i<n; i++)rank[sa[i]]=i;for(i=0; i<n; i++){if(k)k--;j=sa[rank[i]-1];while(s[i+k]==s[j+k])k++;height[rank[i]]=k;}}void solve(){ans=0;//最长字串的长度for(int i=2; i<n; i++){int a1=sa[i-1],a2=sa[i];if(a1>a2)swap(a1,a2);if(a1>=0&&a1<=len1-1&&a2>=len1+1&&a2<=len1+len2){if(ans<height[i]){ans = height[i];pos=a1;}}}}
} sa;
int main()
{scanf("%d",&len1);len2=len1;scanf("%s",sa.s);sa.s[len1]=1;scanf("%s",sa.s+len1+1);sa.n=len1+len2+2;sa.s[len1+len2+1]=0;sa.build_sa(128);sa.build_height();sa.solve();sa.s[pos+ans]=0;printf("%s\n",sa.s+pos);return 0;
}

URAL 1517. Freedom of choice (后缀数组)相关推荐

  1. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  2. Freedom of Choice URAL - 1517

    Background Before Albanian people could bear with the freedom of speech (this story is fully describ ...

  3. URAL - 1297 Palindrome(后缀数组+RMQ)

    题目链接:点击查看 题目大意:给出一个字符串,求出最长的回文子串 题目分析:如果用以往的方法求,不可避免的都需要枚举所有子串,时间复杂度就已经到达了O(n),而后缀数组可以通过O(n)预处理后得到所有 ...

  4. ural 1297 O(nlogn) 后缀数组求最长回文字串

    把原串复制一份反过来接在原串后面,中间用没出现过的字符隔开,然后跑后缀数组,在原创枚举每一个位为回文中心(分奇偶讨论),则回文串长度相当于原串与反串对应位置的lcp,所以先用ST预处理,然后查询.复杂 ...

  5. 【后缀数组】伊芙利特之祭--总结

      前几个月总结的后缀数组专题,现在贴出来吧. 不可重叠重复串; 可重叠k次重复串; 不相同子串个数 最长回文子串 连续重复子串 重复次数最多的连续重复子串 最长公共子串 长度不小于k 的公共子串的个 ...

  6. 寻找一个字符串的重复子串 后缀数组

    什么是后缀数组 令字符串 S=S[1]S[2]...S[n]S=S[1]S[2]...S[n]{\displaystyle S=S[1]S[2]...S[n]} , S[i,j]S[i,j]{\dis ...

  7. 【2012百度之星/资格赛】H:用户请求中的品牌 [后缀数组]

    时间限制: 1000ms 内存限制: 65536kB 描述 馅饼同学是一个在百度工作,做用户请求(query)分析的同学,他在用户请求中经常会遇到一些很奇葩的词汇.在比方说"johnsonj ...

  8. Boring counting HDU - 3518 (后缀数组)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  9. HDU4080 Stammering Aliens(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...

最新文章

  1. t-SNE 原理及Python实例
  2. 烂泥:高负载均衡学习haproxy之关键词介绍
  3. Tarjan算法_LCA
  4. SamplePairing:针对图像处理领域的高效数据增强方式 | PaperDaily #34
  5. 如何防御syn flood的一些思路!
  6. [css] css的user-select:all 有什么用处?
  7. Dom4j完整教程~字符串与XML的转换
  8. 如何理解和运用策略模式
  9. Kafka从上手到实践 - Kafka CLI:Reseting Offset Config CLI | 凌云时刻
  10. vi/vim编辑器使用方法详解
  11. android自动调节亮度是怎么实现的,Android亮度调节的几种实现方法
  12. SVN commit的时候报错conflicted state
  13. 助你快速搭配 Material Design 配色方案的10款Web工具
  14. C 语言实例 - 判断元音/辅音
  15. 【毕业设计】基于单片机的手势检测识别系统 - arduino 物联网嵌入式
  16. 10_放置街灯(Placing Lampposts,UVa 10859)
  17. npm下载什么都报错ERRO -4048
  18. HTML——表单及表单元素
  19. Python实现进度条的5种方式
  20. 网络原理实验7 HDLC及PPP协议

热门文章

  1. DetectoRS论文解读
  2. git 拉取远程仓库分支代码方法
  3. linux centos安装桌面环境,CentOS6 CentOS7装桌面环境
  4. DUMN : Deep User Match Network for Click-Through Rate Prediction
  5. 【学习笔记】状态转移矩阵
  6. OMA Download
  7. 韩国美女连连看核心算法
  8. 记一次lumen直接删除migration文件踩的坑
  9. 2020-10-13 WPS VBA 添加控件,显示“库没有注册”
  10. 从零开始Tableau | 11.表计算-计算依据