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. DiSC小记

    最近在单位进行管理培训时进行了一个DiSC的心理测试,DiSC是Dominance,influence,Steadiness和Conscientiousness四个英文单词的首字母缩写.这四个单词对应 ...

  2. Android JNI&NDK编程小结及建议

    前言 由于网上关于JNI/NDK相关的知识点介绍的比较零散而且不具备参照性,所以写了这篇JNI/NDK笔记,便于作为随时查阅的工具类型的文章,本文主要的介绍了在平时项目中常用的命令.JNI数据类型.签 ...

  3. 【IntelliJ IDEA】spring boot项目在idea实现自动部署

    转载参考自:https://www.cnblogs.com/winner-0715/p/6666579.html spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能 ...

  4. Aspose.Total for .NET 2015 - Unlimited License z

    How to license Aspose.Total for .NET products Add "License.cs" [C#] OR "License.vb&qu ...

  5. linux df -h卡成狗

    linux执行df -h命令,直接卡在,很久没有任何动静 strace df -h跟踪命令执行,查看卡在那里 还有可能就是重做系统吧,检测下内存条是不是坏了等 http://blog.51cto.co ...

  6. MFC中 CDateTimeCtrl 自定义日期显示格式

    MFC里的DateTimePicker控件 ,通过属性来设置的话只能设置两种显示方式,要么日期,要么时间,很多时候我们需要在一个DateTimePicker里日期和时间同时显示. 这个时候只能通过自定 ...

  7. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  8. 关于 Android 平台开发相关的有哪些推荐书籍?

    转自:http://www.zhihu.com/question/19579609 作者:Shan Huang 链接:http://www.zhihu.com/question/19579609/an ...

  9. 百度 api 测试 & python

    ''' 一.文字转语音api,树莓派天气闹钟爬取实时天气数据转换为语音,设置树莓派计划任务 ''' from aip import AipSpeech import requests import r ...

  10. jmeter之怎样减负-实现稳定超高并发測试(性能调优)

    新浪围脖>@o蜗牛快跑o    在測试过程中,刚開始学习的人(也包含早期的我),使用工具不当,加入众多监控组件,很想看到实时报告.跑不了一会,jmeter就卡死甚至oom.仅仅得重新启动.之前的 ...