当前位置:网站首页>URAL1517 Freedom of Choice 【后缀数组:最长公共连续子串】
URAL1517 Freedom of Choice 【后缀数组:最长公共连续子串】
2022-06-29 09:17:00 【叶卡捷琳娜2号】
Freedom of Choice
Time limit: 2.0 second
Memory limit: 64 MB
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
output
THEGREATALBANIA
题意:求两个字符串的最长公共连续子串
正解:连接连个字符串,求出后缀数组和height数组,求出一个公共子串在使其在两个字符串中。
代码:
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = 400000+5;
bool vis[110];
int wa[N],wb[N],wv[N],c[N],height[N],sa[N],pos[N],len[110],ranks[N],s[N];
char str[N];
int n;
bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m){
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[i]=r[i]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[wv[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return ;
}
void calheight(int *r,int *sa,int n){
int i,j,k=0;
for(i=1;i<=n;i++) ranks[sa[i]]=i;
for(i=0;i<n;height[ranks[i++]]=k)
for(k?k--:0,j=sa[ranks[i]-1];r[i+k]==r[j+k];k++);
return ;
}
void solve(int p){
int p1=-1,maxlen=0;
for(int i=2;i<p;i++){
int a1=sa[i-1],a2=sa[i];
if(a1>a2)swap(a1,a2);
if(a1>=0&&a1<n&&a2>=n+1&&a2<2*n+1)
{
if(height[i]>maxlen)
{
maxlen=height[i];
p1=a1;
}
}
}
//cout<<1<<endl;
// cout<<p1<<endl;
for(int i=p1;i<p1+maxlen;i++)
printf("%c",s[i]+'A'-1);
}
int main(){
int t=30;
scanf("%d",&n);
int p=0;
for(int i=1;i<=2;i++){
scanf(" %s",str);
int l=strlen(str);
for(int j=0;j<l;j++) s[p++]=str[j]-'A'+1;
s[p++]=t++;
}
// cout<<p<<endl;
s[p-1]=0;
da(s,sa,p,t);
calheight(s,sa,p-1);
solve(p);
return 0;
}
边栏推荐
- How to traverse objects in the vector container
- Middle order traversal of Li Kou 94 binary tree
- 共用体Union
- 数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
- Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
- Leetcode MySQL database topic 181
- Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
- linux环境下安装配置redis,并设置开机自启动
- After installing anaconda, you need to enter a password to start jupyterlab
- RecyclerView刷新闪烁与删除Item时崩溃问题
猜你喜欢
随机推荐
gcc与makefile
Flutter 基础组件之 Text
Surveiller l'utilisation du pool de connexion des sources de données
Idea debugging fails, reporting jdwp no transports initialized, jvmtierror=agent_ ERROR_ TRANSPORT_ LOAD(196)
How to traverse objects in the vector container
JVM instructions for four call methods
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
Middle order traversal of Li Kou 94 binary tree
C # judge whether the array contains any items of another array
Slider validation code
共用体Union
我想要股票开户优惠,怎么得到?还有,在线开户安全么?
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
2020-09-29 非商品模板化代码层次 rapidjson库
Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
Idea auto completion
2020-09-21 Visual Studio头文件和库目录配置
linux下centos7中mysql5.7安装教程
自定义控件之侧滑关闭 Activity 控件









