博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[URAL-1517][求两个字符串的最长公共子串]
阅读量:7053 次
发布时间:2019-06-28

本文共 3394 字,大约阅读时间需要 11 分钟。

Freedom of Choice

 

 

Background

Before Albanian people could bear with the freedom of speech (this story is fully described in the problem  ), 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
28VOTEFORTHEGREATALBANIAFORYOUCHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA

题意:

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

分析:

  同

#include
#include
#include
using namespace std;const int N=2e5+10;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,0,sizeof c);maxx=256; for(int i=1;i<=len;i++) c[rank[i]=s[i]]++; for(int i=2;i<=maxx;i++) c[i]+=c[i-1]; for(int i=len;i;i--) sa[c[rank[i]]--]=i; trank[sa[1]]=p=1; for(int i=2;i<=len;i++){ if(rank[sa[i]]!=rank[sa[i-1]]) p++; trank[sa[i]]=p; } for(int i=1;i<=len;i++) rank[i]=trank[i]; for(int k=1;p
<<=1,maxx=p){ p=0; for(int i=len-k+1;i<=len;i++) tsa[++p]=i; for(int i=1;i<=len;i++) if(sa[i]>k) tsa[++p]=sa[i]-k; memset(c,0,sizeof c); for(int i=1;i<=len;i++) trank[i]=rank[tsa[i]]; for(int i=1;i<=len;i++) c[trank[i]]++; for(int i=2;i<=maxx;i++) c[i]+=c[i-1]; for(int i=len;i;i--) sa[c[trank[i]]--]=tsa[i]; trank[sa[1]]=p=1; for(int i=2;i<=len;i++){ if(rank[sa[i]]!=rank[sa[i-1]]||rank[sa[i]+k]!=rank[sa[i-1]+k]) p++; trank[sa[i]]=p; } for(int i=1;i<=len;i++) rank[i]=trank[i]; } for(int i=1,k=0;i<=len;i++){ int j=sa[rank[i]-1]; while(s[i+k]==s[j+k]) k++; h[rank[i]]=k;if(k>0) k--; }}int main(){ scanf("%*d"); scanf("%s",s+1);len=strlen(s+1);s[++len]='$';n=len+1; scanf("%s",s+n);len=strlen(s+1);//s[++len]='#'; DA(); for(int i=2;i<=len;i++){ if((sa[i]>=n&&sa[i-1]
=n&&sa[i]

 

转载于:https://www.cnblogs.com/shenben/p/6384171.html

你可能感兴趣的文章
类库与框架,强类型与弱类型的闲聊
查看>>
第一次使用Open Live Writer维护BlogJava
查看>>
SQL Server-流程控制 7,Return 语句
查看>>
你真的会玩SQL吗?查询指定节点及其所有父节点的方法
查看>>
Oracle分析函数的使用
查看>>
Android四个存储数据的SharedPreferences
查看>>
Kafka 客户端实现逻辑分析
查看>>
Python label for _ 用法
查看>>
MySQL bin-log与主从服务器
查看>>
关于异常Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
查看>>
canvas学习笔记一
查看>>
mui选择器和dom获取元素的区别(记得把mui对象转为dom对象才能调用用dom方法)...
查看>>
Windows Mobile 开发系列文章收藏 - 新闻系列
查看>>
【Android】Mac安装EasyTether导致无法识别设备的问题
查看>>
JavaScript 日期联动选择器
查看>>
群用户通过微信小程序可以更好地协作了
查看>>
从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
查看>>
极客DIY:RFID飞贼打造一款远距离渗透利器
查看>>
自动性能统计信息(二)(Automatic Performance Statistics)
查看>>
漏洞真实影响分析,终结网络安全的“狼来了”困境
查看>>