CF 427D Match & Catch 求最短唯一连续LCS
题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次
思路:把2个字符串合并起来求height 后缀数组height的应用
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100010;
char s[maxn];
int sa[maxn];
int t[maxn], t2[maxn], c[maxn];
int rank[maxn], height[maxn];
int l1, l2;
void build_sa(int m, int n)
{
int i, *x = t, *y = t2;
for(i = 0; i < m; i++)
c[i] = 0;
for(i = 0; i < n; i++)
c[x[i] = s[i]]++;
for(i = 1; i < m; i++)
c[i] += c[i-1];
for(i = n-1; i >= 0; i--)
sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1)
{
int p = 0;
for(i = n-k; i < n; i++)
y[p++] = i;
for(i = 0; i < n; i++)
if(sa[i] >= k)
y[p++] = sa[i] - k;
for(i = 0; i < m; i++)
c[i] = 0;
for(i = 0; i < n; i++)
c[x[y[i]]]++;
for(i = 0; i < m; i++)
c[i]+= c[i-1];
for(i = n-1; i >= 0; i--)
sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
if(p >= n)
break;
m = p;
}
} void getHeight(int n)
{
int k = 0;
for(int i = 0; i <= n; i++)
rank[sa[i]] = i;
for(int i = 0; i < n; i++)
{
if(k)
k--;
int j = sa[rank[i]-1];
while(s[i+k] == s[j+k])
k++;
height[rank[i]] = k;
}
} bool ok(int m, int n)
{
int cnt1 = 0, cnt2 = 0;
for(int i = 1; i <= n; i++)
{
if(height[i] >= m)
{
if(sa[i-1] < l1)
cnt1++;
if(sa[i-1] > l1)
cnt2++;
}
else
{
if(sa[i-1] < l1)
cnt1++;
if(sa[i-1] > l1)
cnt2++;
if(cnt1 == 1 && cnt2 == 1)
return true;
cnt1 = cnt2 = 0;
}
}
return false;
}
int main()
{
char a[5555], b[5555];
while(scanf("%s %s", &a, &b) != EOF)
{
l1 = strlen(a);
l2 = strlen(b);
int n = 0;
for(int i = 0; i < l1; i++)
s[n++] = a[i];
s[n++] = 'z'+1;
for(int i = 0; i < l2; i++)
s[n++] = b[i];
s[n] = 0;
build_sa(128, n+1);
getHeight(n); int l = 1, r = 5000;
int ans = -1;
int len = min(l1, l2);
for(int i = 1; i <= len; i++)
if(ok(i, n))
{
ans = i;
break;
}
if(ans <= 0)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}
CF 427D Match & Catch 求最短唯一连续LCS的更多相关文章
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- Codeforces 427D Match & Catch(后缀自动机)
[题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...
- 逆FizzBuzz问题求最短序列
问题描述 FizzBuzz问题:一个大于0的自然数能整除3,将输出“Fizz”:能整除5,将输出“Buzz”:能整除3和5,将输出“FizzBuzz”:否则输出自己. 逆FizzBuzz问题最短序列: ...
- 状压dp,松鼠从起点出发,拿到所有坚果,然后返回起点,求最短时间。
UVA10944 松鼠从起点出发,拿到所有坚果,然后返回起点,求最短时间. #include<iostream> #include<cstdio> #include<al ...
- codeforce D. Shortest Cycle(floyd求最短环)
题目链接:http://codeforces.com/contest/1206/problem/D 给n个点,如果点a[ i ] &a[ j ] 不为0,则点a[ i ] 和 a[ j ] 直 ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- 《github一天一道算法题》:分治法求数组最大连续子序列和
看书.思考.写代码. /*************************************** * copyright@hustyangju * blog: http://blog.csdn. ...
- Task 4 求数组的连续子数组的最大和(团队合作)
小组成员:李敏.刘子晗 1.设计思想:由于已经做过这个题目,只要对之前的程序加上相应的测试和约束即可.我们两个人一起商议后,决定了程序的主框架和并列出了最终可以实现的功能.先要定义数组长度和上下限的变 ...
- Leetcode 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
随机推荐
- Dream------Hbase--0.94版本和0.98/1.X版本api变动
Dream------Hbase--0.94版本和0.98/1.X版本api变动 网上好多说getQualifier.getValue.getRow被..Array代替了,其实并不是的. 1. Int ...
- 总结---Python中的面向对象!
面向对象这种编程的范式每个语言都是通用的,这里总结一下python的面向对象语法,主要包含以下几个方面! 1 基本语法 # 定义类 class Person(object): n = 10 # 类变量 ...
- Coins in a Line I & II
Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...
- MySQL 5.7在线设置复制过滤【转】
转自 MySQL 5.7在线设置复制过滤 - yayun - 博客园 https://www.cnblogs.com/gomysql/p/4991197.html 5.7也GA了,有许多新的特性,其中 ...
- linux常用运维命令【转】
自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量.看看有没有黑阔搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试! 1.查看有多少个IP访问: awk ...
- 数组slice方法
slice slice(start,end):方法可从已有数组中返回选定的元素,返回一个新数组,包含从start到end(不包含该元素)的数组元素.(不会改变原数组) start参数:必须,规定从何处 ...
- MVC母板页传递数据
问题:判断Session 失败 结果: if (Session[SessionKey.AccountInfo] != null) { ViewData["AdminUser"] = ...
- Codeforces Round #533 (Div. 2) E - Helping Hiasat 最大团
E - Helping Hiasat 裸的最大团,写了一种 2 ^ (m / 2) * (m / 2)的复杂度的壮压, 应该还有更好的方法. #include<bits/stdc++.h> ...
- VMware虚拟机三种联网方法及原理
VMware虚拟机三种联网方法及原理 一.Brigde——桥接:默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时 ...
- jbox小型交互表单(ajax)
function updateval(id){ var url = '${ctx}/zbapp/ranking/zbappRankingList/form?id=${zbappRankingList. ...