Freedom of Choice

URAL - 1517

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 output
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA

题意:

  给出两个字符串,求 最长公共子串

分析:

  同POJ2774 Long Long Message

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=2e5+;
int n,len,id,ans,maxx,sa[N],tsa[N],rank[N],trank[N],h[N],c[N];
char s[N];
void DA(){
int p;
memset(c,,sizeof c);maxx=;
for(int i=;i<=len;i++) c[rank[i]=s[i]]++;
for(int i=;i<=maxx;i++) c[i]+=c[i-];
for(int i=len;i;i--) sa[c[rank[i]]--]=i;
trank[sa[]]=p=;
for(int i=;i<=len;i++){
if(rank[sa[i]]!=rank[sa[i-]]) p++;
trank[sa[i]]=p;
}
for(int i=;i<=len;i++) rank[i]=trank[i];
for(int k=;p<len;k<<=,maxx=p){
p=;
for(int i=len-k+;i<=len;i++) tsa[++p]=i;
for(int i=;i<=len;i++) if(sa[i]>k) tsa[++p]=sa[i]-k;
memset(c,,sizeof c);
for(int i=;i<=len;i++) trank[i]=rank[tsa[i]];
for(int i=;i<=len;i++) c[trank[i]]++;
for(int i=;i<=maxx;i++) c[i]+=c[i-];
for(int i=len;i;i--) sa[c[trank[i]]--]=tsa[i];
trank[sa[]]=p=;
for(int i=;i<=len;i++){
if(rank[sa[i]]!=rank[sa[i-]]||rank[sa[i]+k]!=rank[sa[i-]+k]) p++;
trank[sa[i]]=p;
}
for(int i=;i<=len;i++) rank[i]=trank[i];
}
for(int i=,k=;i<=len;i++){
int j=sa[rank[i]-];
while(s[i+k]==s[j+k]) k++;
h[rank[i]]=k;if(k>) k--;
}
}
int main(){
scanf("%*d");
scanf("%s",s+);len=strlen(s+);s[++len]='$';n=len+;
scanf("%s",s+n);len=strlen(s+);//s[++len]='#';
DA();
for(int i=;i<=len;i++){
if((sa[i]>=n&&sa[i-]<n)||(sa[i-]>=n&&sa[i]<n)){
if(ans<h[i]){
ans=h[i];
id=sa[i];
}
}
}
s[id+ans]=;
puts(s+id);
return ;
}

[URAL-1517][求两个字符串的最长公共子串]的更多相关文章

  1. 求两个字符串的最长公共子串——Java实现

    要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...

  2. 求两个字符串的最长公共子串(LCS)

    http://tianyunpu2008.blog.163.com/blog/static/6559379920089162236915/

  3. 【java】求两个字符串的最长公共子串

    这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不 ...

  4. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

  5. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  6. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  7. 【Java例题】5.5 两个字符串中最长公共子串

    5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...

  8. hihocoder-1415 后缀数组三·重复旋律3 两个字符串的最长公共子串

    把s1,s2拼接,求Height.相邻的Height判断左右串起点是否在两个串中,另外对Height和s1.length()-SA[i-1]取min. #include <iostream> ...

  9. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

随机推荐

  1. interview ms1 N_Dorm

    判断是否为n回文,比如 a b a 是1 回文, abcdab是2回文. 输入: abcabc|3 这种格式,输出true or false #include <iostream> #in ...

  2. 搜狗拼音输入法LINUX版安装

    搜狗拼音输入法LINUX版官方下载: http://pinyin.sogou.com/linux/?r=pinyin 一.添加fcitx的nightlyPPA 在终端中输入: sudo add-apt ...

  3. EOJ 3.30 A. 打工时不可能打工的【贪心】

    [链接]:https://acm.ecnu.edu.cn/contest/59/problem/A/ A. 打工时不可能打工的 Time limit per test: 2.0 seconds Mem ...

  4. 创建一个vue-cli项

    一.vue cli脚手架 Vue 提供了一个官方的cli,为单页面应用 (SPA) 快速搭建繁杂的脚手架,通过这个工具我们就可以很方便的来创建一个基于vue的项目. 二.安装一些必要的东西node.n ...

  5. java的架构流行阶段

    第一阶段:SSM 第二阶段:分布式系统改造,平台化初具规模,各项垂直业务系统搭建上线.产品端极大丰富用户投资.大数据平台研究并使用 第三阶段:SOA治理,使用zookeeper作为注册中心,dubbo ...

  6. ural 1519 fomular 1 既插头DP学习笔记

    直接看CDQ在2008年的论文吧. 个人认为她的论文有两个不明确的地方, 这里补充一下: 首先是轮廓的概念. 我们在进行插头DP时, 是从上往下, 从左往右逐个格子进行的, 已经处理的格子与未经处理的 ...

  7. linux yum 安装软件

    概括了部分常用的yum命令包括: 自动搜索最快镜像插件:yum install yum-fastestmirror安装yum图形窗口插件:yum install yumex 1 安装yum insta ...

  8. Jinja2文档学习

    这些文档需要精度一遍 1.http://jinja.pocoo.org/docs/dev/ 2.http://jinja.pocoo.org/docs/dev/templates/# 3.https: ...

  9. css :before 和 :after

    :before p:before 在每个 <p> 元素的内容之前插入内容. 2 :after p:after 在每个 <p> 元素的内容之后插入内容. 2 <!DOCTY ...

  10. oracle12安装软件后安装数据库,然后需要自己配置监听

    oracle12安装软件后安装数据库,然后需要自己配置监听 没想到你是这样的oracle12: 不能同时安装软件和数据库,分别安装之后,\NETWORD\ADMIN\下面竟然没有listener.or ...