URAL 1517 Freedom of Choice
Freedom of Choice
This problem will be judged on Ural. Original ID: 1517
64-bit integer IO format: %lld Java class name: (Any)
Background
Problem
Input
Output
Sample Input
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
Sample Output
THEGREATALBANIA
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn];
bool cmp(int *r,int i,int j,int k){
return r[i] == r[j] && r[i+k] == r[j+k];
}
void da(int *r,int *sa,int n,int m){
int i,k,p,*x = rk,*y = wb;
for(i = ; i < m; ++i) wd[i] = ;
for(i = ; i < n; ++i) wd[x[i] = r[i]]++;
for(i = ; i < m; ++i) wd[i] += wd[i-];
for(i = n-; i >= ; --i) sa[--wd[x[i]]] = i; for(p = k = ; p < n; k <<= , m = p){
for(p = ,i = n-k; i < n; ++i) y[p++] = i;
for(i = ; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < n; ++i) wv[i] = x[y[i]];
for(i = ; i < m; ++i) wd[i] = ;
for(i = ; i < n; ++i) wd[wv[i]]++;
for(i = ; i < m; ++i) wd[i] += wd[i-];
for(i = n-; i >= ; --i) sa[--wd[wv[i]]] = y[i]; swap(x,y);
x[sa[]] = ;
for(p = i = ; i < n; ++i)
x[sa[i]] = cmp(y,sa[i-],sa[i],k)?p-:p++;
}
}
void calcp(int *r,int *sa,int n){
for(int i = ; i <= n; ++i) rk[sa[i]] = i;
int h = ;
for(int i = ; i < n; ++i){
if(h > ) h--;
for(int j = sa[rk[i]-]; j+h < n && i+h < n; ++h)
if(r[i+h] != r[j+h]) break;
lcp[rk[i]-] = h;
}
}
char sc[maxn],sb[maxn];
int sa[maxn],r[maxn];
int main() {
int len;
while(~scanf("%d",&len)){
scanf("%s",sc);
scanf("%s",sb);
sc[len] = '\0';
strcpy(sc+len+,sb);
for(int i = ; i < (len<<|); ++i)
r[i] = sc[i];
r[len<<|] = ;
da(r,sa,(len<<)+,);
calcp(r,sa,len<<|);
int ans = ,index = ;
for(int i = ; i < (len<<|); ++i){
if(sa[i] < len != sa[i+] < len){
if(lcp[i] > ans){
ans = lcp[i];
index = sa[i];
}
}
}
for(int i = ; i < ans; ++i)
putchar(sc[index+i]);
putchar('\n');
}
return ;
}
URAL 1517 Freedom of Choice的更多相关文章
- URAL 1517 Freedom of Choice(后缀数组,最长公共字串)
题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...
- URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whyorwhnt/article/details/34075603 题意:给出两个串的长度(一样长) ...
- Ural 1517. Freedom of Choice 后缀数组
Ural1517 所谓后缀数组, 实际上准确的说,应该是排序后缀数组. 一个长度为N的字符串,显然有N个后缀,将他们放入一个数组中并按字典序排序就是后缀数组的任务. 这个数组有很好的性质,使得我们运行 ...
- URAL1517Freedom of Choice(后缀数组)
Background Before Albanian people could bear with the freedom of speech (this story is fully describ ...
- 后缀数组 & 题目
后缀数组被称为字符串处理神器,要解决字符串问题,一定要掌握它.(我这里的下标全部都是从1开始) 首先后缀数组要处理出两个数组,一个是sa[],sa[i]表示排名第i为的后缀的起始位置是什么,rank[ ...
- [URAL-1517][求两个字符串的最长公共子串]
Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...
- 学券制(教育券、school voucher)
美国「学券制」是怎样的一种制度?它为什么是共和党的执政政策?它在美国及其它地区有实施吗?效果如何?能否在保证公平的同时,通过市场提高教育质量? 作者:冉筱韬链接:https://www.zhihu.c ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
- TMS X-Cloud Todolist with FNC
Wednesday, June 22, 2016 It's almost three months since we released the first version of the TMS FNC ...
随机推荐
- 跳一跳的Python环境搭建
微信最新的小程序里面出了个叫“跳一跳”的小游戏,一经推出立马刷爆了朋友圈,而一些大神们也通过Python实现了自动玩游戏具体代码见(Github地址:https://github.com/wangsh ...
- 修改struts2自定义标签的源代码,在原有基础上增加功能(用于OA项目权限判断,是否显示某个权限)
OA项目在做权限判断时 原始方式: 现在完成的功能 :通过改变struts2自定标签源代码 在原有的基础上 增加判断权限的功能 而页面上使用标签的方式 还是下图 步骤: 打开文件 搜索< ...
- [SharePoint][SharePoint2013循序渐进]SPS2013简介
本章概要: 1.啥是SPS2013 2.SharePoint如何作用于团队协作和信息共享 3.SP2013有哪些用户权限 4.什么是SharePoint2013 online 5.SP在内部署和在线订 ...
- POJ 3709
简单的单调队列优化,注意是哪些点加入队列即可. #include <iostream> #include <cstdio> #include <algorithm> ...
- 请用Java设计一个Least Recently Used (LRU) 缓存
LRU介绍:LRU是Least Recently Used的缩写,即最少使用页面置换算法,是为虚拟页式存储管理服务的, 思路介绍: 能够使用两个标准的数据结构来实现.Map和Queue.由于须要支持多 ...
- 多年iOS开发经验总结(一)
总结了几个月的东西终于能和大家分享了,不多说,直接看东西! 1.禁止手机睡眠 1 [UIApplication sharedApplication].idleTimerDisabled = YES; ...
- Bata版本
一.团队成员 1)冯鹏(组长) 201731062617 2)鲜泽 201731062612 3)李家豪 201731062614 4)郭经伟 201731062615 5)程前勇 2017310 ...
- 浅谈javascript的面向对象思想
面向对象的三大基本特性 封装(把相关的信息(无论数据或方法)存储在对象中的能力) 继承(由另一个类(或多个类)得来类的属性和方法的能力) 多态(一个对象在不同情况下的多种形态) 定义类或对象 第一种: ...
- [OS][ linux ] 建立新帳號, 變更密碼, 加入 sudoer
新增 linux , 設定預設 password, 新增 user 到 sudoers 1. 新增 User sudo useradd aa97 2. 設定 User password sudo pa ...
- table标签 在谷歌和ie浏览器下不同的表现效果
在项目中有了一个这样的需求: 我需要利用vue的模板语法v-for循环生成tr,这个tr是需要双重循环来确定其个数的, 我的实现: 我在tr外面包了一个template标签, 效果: 谷歌浏览器下实现 ...