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 ;
}

URAL 1517 Freedom of Choice的更多相关文章

  1. URAL 1517 Freedom of Choice(后缀数组,最长公共字串)

    题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...

  2. URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whyorwhnt/article/details/34075603 题意:给出两个串的长度(一样长) ...

  3. Ural 1517. Freedom of Choice 后缀数组

    Ural1517 所谓后缀数组, 实际上准确的说,应该是排序后缀数组. 一个长度为N的字符串,显然有N个后缀,将他们放入一个数组中并按字典序排序就是后缀数组的任务. 这个数组有很好的性质,使得我们运行 ...

  4. URAL1517Freedom of Choice(后缀数组)

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

  5. 后缀数组 & 题目

    后缀数组被称为字符串处理神器,要解决字符串问题,一定要掌握它.(我这里的下标全部都是从1开始) 首先后缀数组要处理出两个数组,一个是sa[],sa[i]表示排名第i为的后缀的起始位置是什么,rank[ ...

  6. [URAL-1517][求两个字符串的最长公共子串]

    Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...

  7. 学券制(教育券、school voucher)

    美国「学券制」是怎样的一种制度?它为什么是共和党的执政政策?它在美国及其它地区有实施吗?效果如何?能否在保证公平的同时,通过市场提高教育质量? 作者:冉筱韬链接:https://www.zhihu.c ...

  8. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

  9. TMS X-Cloud Todolist with FNC

    Wednesday, June 22, 2016 It's almost three months since we released the first version of the TMS FNC ...

随机推荐

  1. 跳一跳的Python环境搭建

    微信最新的小程序里面出了个叫“跳一跳”的小游戏,一经推出立马刷爆了朋友圈,而一些大神们也通过Python实现了自动玩游戏具体代码见(Github地址:https://github.com/wangsh ...

  2. 修改struts2自定义标签的源代码,在原有基础上增加功能(用于OA项目权限判断,是否显示某个权限)

    OA项目在做权限判断时  原始方式: 现在完成的功能 :通过改变struts2自定标签源代码   在原有的基础上  增加判断权限的功能  而页面上使用标签的方式 还是下图 步骤: 打开文件 搜索< ...

  3. [SharePoint][SharePoint2013循序渐进]SPS2013简介

    本章概要: 1.啥是SPS2013 2.SharePoint如何作用于团队协作和信息共享 3.SP2013有哪些用户权限 4.什么是SharePoint2013 online 5.SP在内部署和在线订 ...

  4. POJ 3709

    简单的单调队列优化,注意是哪些点加入队列即可. #include <iostream> #include <cstdio> #include <algorithm> ...

  5. 请用Java设计一个Least Recently Used (LRU) 缓存

    LRU介绍:LRU是Least Recently Used的缩写,即最少使用页面置换算法,是为虚拟页式存储管理服务的, 思路介绍: 能够使用两个标准的数据结构来实现.Map和Queue.由于须要支持多 ...

  6. 多年iOS开发经验总结(一)

    总结了几个月的东西终于能和大家分享了,不多说,直接看东西! 1.禁止手机睡眠 1 [UIApplication sharedApplication].idleTimerDisabled = YES; ...

  7. Bata版本

    一.团队成员 1)冯鹏(组长) 201731062617 2)鲜泽   201731062612 3)李家豪 201731062614 4)郭经伟 201731062615 5)程前勇 2017310 ...

  8. 浅谈javascript的面向对象思想

    面向对象的三大基本特性 封装(把相关的信息(无论数据或方法)存储在对象中的能力) 继承(由另一个类(或多个类)得来类的属性和方法的能力) 多态(一个对象在不同情况下的多种形态) 定义类或对象 第一种: ...

  9. [OS][ linux ] 建立新帳號, 變更密碼, 加入 sudoer

    新增 linux , 設定預設 password, 新增 user 到 sudoers 1. 新增 User sudo useradd aa97 2. 設定 User password sudo pa ...

  10. table标签 在谷歌和ie浏览器下不同的表现效果

    在项目中有了一个这样的需求: 我需要利用vue的模板语法v-for循环生成tr,这个tr是需要双重循环来确定其个数的, 我的实现: 我在tr外面包了一个template标签, 效果: 谷歌浏览器下实现 ...