后缀数组。

求多个字符串翻转与否中最长公共子串长。

二分答案,反过来多建一倍的字符串,二分时特判一下即可。

By:大奕哥

 #include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#define rank fank
using namespace std;
const int N=;
int r[N],wa[N],wb[N],wv[N],wu[N],sa[N],rank[N],height[N],bel[N],cnt;
int 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;int *x=wa,*y=wb;
for(i=;i<m;++i)wu[i]=;
for(i=;i<n;++i)wu[x[i]=r[i]]++;
for(i=;i<m;++i)wu[i]+=wu[i-];
for(i=n-;i>=;--i)sa[--wu[x[i]]]=i;
for(j=,p=;p<n;j<<=,m=p)
{
for(p=,i=n-j;i<n;++i)y[p++]=i;
for(i=;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<n;++i)wv[i]=x[y[i]];
for(i=;i<m;++i)wu[i]=;
for(i=;i<n;++i)wu[wv[i]]++;
for(i=;i<m;++i)wu[i]+=wu[i-];
for(i=n-;i>=;--i)sa[--wu[wv[i]]]=y[i];
for(swap(x,y),p=,x[sa[]]=,i=;i<n;++i)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
}
void calcHeight(int *rank,int *sa,int n)
{
int i,j,k=;
for(i=;i<=n;++i)rank[sa[i]]=i;
for(i=;i<n;height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k];++k);
return ;
}
char s[];
bool v[];
int tot,num;
bool check(int x,int n)
{
cnt=;memset(v,,sizeof(v));
for(int i=;i<=n;++i)
{
if(height[i]<x){cnt=;memset(v,,sizeof(v));}
else
{
if(v[bel[sa[i-]]]==)cnt++,v[bel[sa[i-]]]=;
if(!v[bel[sa[i]]])cnt++,v[bel[sa[i]]]=;
if(cnt>=num)return ;
}
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
int len=;int p=;
for(int i=;i<num;++i)
{
scanf("%s",s);
int n=strlen(s);
for(int j=;j<n;++j)
{
r[len]=s[j];bel[len]=i;++len;
}
r[len]=+p;bel[len]=+p;++len;++p;
for(int j=n-;j>=;--j)
{
r[len]=s[j];bel[len]=i;++len;
}
r[len]=+p;bel[len]=+p;++len;++p;
}
--len;r[len]=;
da(r,sa,len+,);
calcHeight(rank,sa,len);
int ans=,l=,r=len;
while(l<=r)
{
int mid=l+r>>;
if(check(mid,len))ans=mid,l=mid+;
else r=mid-;
}
printf("%d\n",ans);
}
return ;
}

POJ1226 Substrings的更多相关文章

  1. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  2. POJ1226:Substrings(后缀数组)

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

  3. POJ1226 - Substrings(KMP+二分)

    题目大意 给定n个字符串,字符串可逆序可顺序,求它们的最长公共子串 题解 在输入的过程中记录一下最短的那个字符串,然后枚举起点,然后进行二分求出子串末位置,然后再验证是否是公共子串,记录最长的公共子串 ...

  4. POJ1226 Substrings(二分+后缀数组)

    题意:给n个字符串,求最长的子串,满足它或它的逆置出现在所有的n个字符串中. 把n个字符串及其它们的逆置拼接,中间用不同字符隔开,并记录suffix(i)是属于哪个字符串的: 跑后缀数组计算heigh ...

  5. [POJ1226]Substrings(后缀数组)

    传送门 给定 n 个字符串,求出现或反转后出现在每个字符串中的最长子串. 算法分析: 这题不同的地方在于要判断是否在反转后的字符串中出现.其实这并没有加大题目的难度. 只需要先将每个字符串都反过来写一 ...

  6. 【POJ1226】Substrings(后缀数组,二分)

    题意: n<=10,len<=100 思路: 只有一个字符串的时候特判一下 #include<cstdio> #include<cstring> #include& ...

  7. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  8. Leetcode: Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  9. CSU-1632 Repeated Substrings (后缀数组)

    Description String analysis often arises in applications from biology and chemistry, such as the stu ...

随机推荐

  1. 转:国内优秀npm镜像推荐及使用

    原文:http://riny.net/2014/cnpm/ npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便 ...

  2. struts获得参数(属性,对象,模型驱动)

    0. strutsMVC

  3. Struts访问servletAPI方式

    1.原理

  4. mysql安装后开启远程

    操作系统为centos7 64 1.修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2. ...

  5. Linux 编译 apr-util 时报错

    前言 Apache 2.4 以后的版本不再自带 APR 库(Apache Portable Runtime,Apache 可移植运行库),所以在安装 Apache 之前需要手动下载安装 APR 库. ...

  6. 在Linux(CentOS)中安装.netcore SDK

    官方链接 :https://dotnet.microsoft.com/download/linux-package-manager/centos/sdk-current 可以直接根据官方链接,选择Li ...

  7. 14个最常见的Kafka面试题及答案【转】

    原创 IT168企业级 2017-08-21 17:40 本文为您盘点了14个最常见的Kafka面试题,同时也是对Apache Kafka初学者必备知识点的一个整理与介绍. 1.请说明什么是Apach ...

  8. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  9. Enumeration的学习

    枚举是jdk5.0之后的新特性.枚举的使用在编程中能起到很大的作用,本文从枚举的适用范围.枚举的特点.枚举的使用等三个方面学习枚举 一.枚举的使适用范围 “在有限的范围内选择值”:比如一个星期只有星期 ...

  10. leetcode 之Search in Rotated Sorted Array(四)

    描述 Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?    Would this aff ...