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 ...
随机推荐
- LinkedList 注意事项
public E getFirst() 返回此列表的第一个元素. public E getLast() 返回此列表的最后一个元素. public E removeFirst() 移除并返回此列表的 ...
- MyBatis-Spring-SqlSessionFactoryBean(转)
SqlSessionFactoryBean 在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中 ...
- [Beginning SharePoint Designer 2010]Chapter2 编辑页面
本章概要: 1.如何展开隐藏任务面板和ribbon标签 2.页面编辑模式 3.代码视图模式和智能提示 4.如何组合SharePoint和页面上其他元素
- C++类中静态变量和静态方法的注意事项
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量仅仅存储一份供全部对象共用.所以在全部对象中都能够共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还能够 ...
- TS3
let [first, ...rest] = [1, 2, 3, 4]; console.log(first); // outputs 1 console.log(rest); // outputs ...
- 51nod-1462: 树据结构
[传送门:51nod-1462] 简要题意: 给出一棵n个点的树,每个点有两个权值v,t 有Q个操作,有两种操作: 1.将x到根上的路径上的点的v值都加上d 2.将x到根上的路径上的点的t值都加上每个 ...
- 新东方雅思词汇---6.1、oppose
新东方雅思词汇---6.1.oppose 一.总结 一句话总结:op(相反)+pos(放) 英 [ə'pəʊz] 美 [ə'poz] vt. 反对:对抗,抗争 vi. 反对 [ 过去式 oppos ...
- 17.UNP第一章 简介
获取时间的客户端代码: //客户端程序 #include "unp.h" int main(int argc, char **argv) { int sockfd, n; ]; s ...
- js写发布微博文本框---2017-04-14
实现效果: 1.文本框输入内容,低端字数对应减少 2.当文本框内容超出时,会显示字数超出多少 效果图如下: 实现代码: <!DOCTYPE html><html> <he ...
- C#比较二个数组并找出相同或不同元素的方法
这篇文章主要介绍了C#比较二个数组并找出相同或不同元素的方法,涉及C#针对数组的交集.补集等集合操作相关技巧,非常简单实用, 具有一定参考借鉴价值,需要的朋友可以参考下 " }; " ...