URAL第一A。。。。后缀数组第一题。。。。。
大白书上关于后缀数组的部分好多bug。。。。。。
总算被我一一坑过去了。。。。。
搞会了后缀数组的正确姿势。。。。
Time Limit:2000MS |
|
Memory Limit:65536KB |
|
64bit IO Format:%I64d & %I64u |
[Submit] [Go
Back] [Status]
Description
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 numberN(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
ofNcapital 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
input
output
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
|
THEGREATALBANIA
|
Source
Problem Author:Ilya Grebnov, Nikita Rybak, Dmitry Kovalioff Problem Source:Timus Top Coders: Third Challenge
[Submit] [Go
Back] [Status]
|
 |
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=4e5+10;
char s[N];
int sa[N],rank[N],rank2[N],height[N],c[N],*x,*y;
void radix_sort(int n,int sz)
{
memset(c,0,sizeof(c));
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<sz;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
}
void get_sa(char* s,int n,int sz=128)
{
x=rank;y=rank2;
for(int i=0;i<n;i++)
x[i]=s[i],y[i]=i;
radix_sort(n,sz);
for(int len=1;len<n;len<<=1)
{
int yid=0;
for(int i=n-len;i<n;i++) y[yid++]=i;
for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len;
radix_sort(n,sz);
swap(x,y);
x[sa[0]]=yid=0;
for(int i=1;i<n;i++)
{
if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n
&&y[sa[i-1]+len]==y[sa[i]+len])
x[sa[i]]=yid;
else x[sa[i]]=++yid;
}
sz=yid+1;
if(sz>=n) break;
}
for(int i=0;i<n;i++)
rank[i]=x[i];
}
void get_height(char* s,int n)
{
int k=0;
for(int i=0;i<n;i++)
{
if(rank[i]==0) continue;
k=max(0,k-1);
int j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
s[n]=127;
scanf("%s",s+n+1);
int nn=strlen(s);
get_sa(s,nn);
get_height(s,nn);
int be=0,len=0;
for(int i=1;i<nn;i++)
{
if((sa[i]<n&&sa[i-1]>n)||(sa[i]>n&&sa[i-1]<n))
{
if(height[i]>len)
{
be=sa[i]; len=height[i];
}
}
}
for(int i=be;i<be+len;i++) putchar(s[i]);
putchar(10);
}
return 0;
}
分享到:
相关推荐
《Ural URAL 解题思路》 在编程竞赛和算法挑战的世界中,Ural University的在线判题系统,简称Ural或URAL,是许多程序员磨炼技能的重要平台。它提供了丰富的算法题目,涵盖数据结构、图论、动态规划、数学等多个...
"Ural"是一款字体,可能是指一种特定的中文字体或者西文字体设计。在IT领域,字体扮演着至关重要的角色,特别是在图形设计、网页设计、出版印刷以及各种视觉传达中。字体的选择能够极大地影响信息的呈现效果和用户...
标题 "acm_ural_1148" 指向的是一个 ACM(国际大学生程序设计竞赛)问题,这个问题在 Ural University Online Judge(乌拉尔大学在线评测系统)上编号为 1148。描述中的 "Pascal acm_timus_ural_1148.pas" 表明提供...
"URAL3D"是一个与字体相关的主题,这通常指的是一个特定的三维字体设计或字体库。在计算机领域,字体是用于显示和打印文本的图形表示。它们由一系列字符形状组成,包括字母、数字和标点符号,每个都有其独特的样式和...
URAL部分测试数据是针对Timus Online Judge平台的一个竞赛或练习问题的数据集。Timus Online Judge是一个流行的在线编程竞赛和练习平台,它为参赛者提供了一系列的编程题目,以提升编程技能和算法理解能力。这个数据...
"ural部分题解"是一个关于编程竞赛平台Ural(也称为URAL Online Judge或University of Maribor Algorithmic contests)的解题集,由大牛出品。这个资源包含Vol1到Vol3的部分题目解答,虽然不完整,但大约涵盖了200道...
【标题】"acm_ural_1099" 是一个与编程竞赛相关的主题,通常在ACM(国际大学生程序设计竞赛)或类似的算法比赛中出现。这类问题旨在测试参赛者解决算法问题的能力,通常需要使用一种或多种编程语言来编写解决方案。 ...
Ural 1238 源代码 涉及算法(动态规划、贪心、DFS)
HDU、PKU和URAL是三个著名的在线编程竞赛平台,它们为程序员提供了丰富的算法题目,帮助参赛者提升编程技能和算法理解能力。这些平台的题目通常涵盖了基础算法、数据结构、数学逻辑等多个方面,对于准备各类编程竞赛...
《URAL题解》是针对ACM(国际大学生程序设计竞赛)中URAL在线判题系统题库的详尽解析,涵盖了从Vol_I到Vol_III的所有题目。这些文档旨在帮助参赛者理解和解决各种算法问题,提升编程技能,为比赛做好充分准备。 ...
Ural ACM通常指的是乌拉尔大学(University of Ural)举办的算法竞赛,这类比赛旨在提升参赛者在算法设计、数据结构以及问题解决能力上的技能。在这个压缩包中,我们有针对Ural ACM 1000题目的解决方案,这些代码...
**Ural开源库详解** Ural是一个开源的C++库,专为实现高效、灵活的线性代数计算而设计。这个库的核心特性是利用模板类来表示不同等级的张量,包括标量、向量(等级1)、矩阵(等级2)以及更高阶的张量(如等级3)。...
URAL-PHA是一个与字体相关的主题。在这个压缩包中,我们看到只有一个文件名“2238”,这可能是某种特定字体的文件或者一个包含了多个字体文件的集合。在IT行业中,字体设计是用户界面和视觉传达的重要组成部分,尤其...
for each neighbor w of v: if distance[v] + weight(v, w) [w]: distance[w] = distance[v] + weight(v, w) end while return distance[] ``` #### 1005 O **题目概述:** 这是一道关于动态规划的题目,...
资源分类:Python库 所属语言:Python 资源全名:ural-0.28.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
"ural"是一个与SCSS相关的项目,SCSS是Sass(Syntactically Awesome Style Sheets)的缩写,是一种预处理器语言,它扩展了CSS,增加了变量、嵌套规则、混合、函数等特性,使CSS编写更加简洁和可维护。在"ural-master...
28. The Theoretical Background of the Ural-Altale Hypothesis The hypothesis of common origin of the Altaic languages,and even the Ural-Altaic languages,dates from the first half of the last century....
chieves t he integration of optical and st ruct ural design sof tware. Mainly based on black box compo2 nent met hod , t he development of optical and st ruct ural design sof tware is completed using ...
- **URAL 1019**:此题要求计算区间覆盖的颜色,并统计最长相同颜色的区间。这需要在线段树中额外维护一些状态信息,比如每个区间被涂色的次数。 - **ZOJ 2301**:与URAL 1019类似,但这里是针对点进行染色操作。在...
and the National Geographic Society[5] as 4/5 of the landmass of Eurasia – with the western portion of the latter occupied by Europe – located to the east of the Suez Canal, east of the Ural ...