POJ1226:Substrings(后缀数组)
Description
Input
by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
Output
Sample Input
2
3
ABCD
BCDFF
BRCD
2
rose
orchid
Sample Output
2
2
Source
且没有出如今字符串中的字符隔开,再将n个字符串所有连起来,中间也是用一
个互不同样的且没有出如今字符串中的字符隔开,求后缀数组。然后二分答案。
再将后缀分组。推断的时候,要看是否有一组后缀在每一个原来的字符串或反转后
的字符串中出现。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
int wa[N],wb[N],wsf[N],wv[N],sa[N];
int rank[N],height[N],s[N],a[N];
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)//n要包括末尾加入的0
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i<m; i++) wsf[i]=0;
for(i=0; i<n; i++) wsf[x[i]=r[i]]++;
for(i=1; i<m; i++) wsf[i]+=wsf[i-1];
for(i=n-1; i>=0; i--) sa[--wsf[x[i]]]=i;
p=1;
j=1;
for(; p<n; j*=2,m=p)
{
for(p=0,i=n-j; i<n; i++) y[p++]=i;
for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0; i<n; i++) wv[i]=x[y[i]];
for(i=0; i<m; i++) wsf[i]=0;
for(i=0; i<n; i++) wsf[wv[i]]++;
for(i=1; i<m; i++) wsf[i]+=wsf[i-1];
for(i=n-1; i>=0; i--) sa[--wsf[wv[i]]]=y[i];
t=x;
x=y;
y=t;
x[sa[0]]=0;
for(p=1,i=1; i<n; i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
}
}
void getheight(int *r,int n)//n不保存最后的0
{
int i,j,k=0;
for(i=1; i<=n; i++) rank[sa[i]]=i;
for(i=0; i<n; i++)
{
if(k)
k--;
else
k=0;
j=sa[rank[i]-1];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
} char str[N];
int len[105],size,ans[N],id[N];
bool vis[105]; bool check(int mid,int n,int k)
{
int i,j;
int size = 0,cnt = 0;
MEM(vis,false);
for(i = 1; i<=n; i++)
{
if(height[i]>=mid)
{
for(j = 0; j<k; j++)
{
if(id[sa[i]]==j) cnt+=(vis[j]?0:1),vis[j]=true;
if(id[sa[i-1]]==j) cnt+=(vis[j]? 0:1),vis[j]=true;
}
}
else
{
if(cnt>=k) return true;
cnt = 0;
MEM(vis,false);
}
}
if(cnt>=k) return true;
return false;
} int main()
{
int n,k,i,j,flag = 0,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&k);
n = 0;
size = 0;
int p = 1;
for(i = 0; i<k; i++)
{
scanf("%s",str);
int ll = strlen(str);
for(j = 0; j<ll; j++)
{
id[n] = i;
s[n++] = str[j];
}
s[n++] = '#'+(p++);
for(j = ll-1; j>=0; j--)
{
id[n] = i;
s[n++] = str[j];
}
s[n++] = '#'+(p++);
}
s[n-1] = 0;
getsa(s,sa,n,255);
getheight(s,n-1);
int l=1,r=n,mid,ans = 0;
while(l<=r)
{
mid = (l+r)/2;
if(check(mid,n,k))
{
ans = mid;
l = mid+1;
}
else r = mid-1;
}
printf("%d\n",ans);
} return 0;
}
POJ1226:Substrings(后缀数组)的更多相关文章
- POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串
题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS Memory Limit: 10000K Total ...
- UVALive - 6869 Repeated Substrings 后缀数组
题目链接: http://acm.hust.edu.cn/vjudge/problem/113725 Repeated Substrings Time Limit: 3000MS 样例 sample ...
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- SPOJ- Distinct Substrings(后缀数组&后缀自动机)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- SPOJ DISUBSTR Distinct Substrings 后缀数组
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...
- ●SPOJ 8222 NSUBSTR - Substrings(后缀数组)
题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 同届红太阳 --WSY给出的后缀数组解法!!! 首先用倍增算法求出 sa[i],rak[i],hei[i]然 ...
随机推荐
- DIV与SPAN的区别
DIV 和 SPAN 元素最大的特点是默认都没有对元素内的对象进行任何格式化渲染.主要用于应用样式表(共同点). 两者最明显的区别在于DIV是块元素,而SPAN是行内元素(也译作内嵌元素). 详解:1 ...
- zlib的安装
wget http://www.zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure mak ...
- 用nifi把hdfs数据导到hive
全景图: 1. ListHDFS & FetchHDFS: ListHDFS: FetchHDFS: 2. EvaluateJsonPath: {"status&qu ...
- 开发笔记:用不用UnitOfWork以及Repository返回什么集合类型
这2天实际开发中明确的东西,在这篇博文中记录一下. 之前对是否需要自己封装UnitOfWork有些犹豫,因为Entity Framework就是一个UnitOfWork实现, 自己再封装一下显得有些多 ...
- Linux: shell常用通配符
字符 含义 * 匹配 0 或多个字符 ? 匹配任意一个字符 [list] 匹配 list 中的任意单一字符 [!list] 匹配 除list 中的任意单一字符以外的字符 [c1-c2] 匹配 c1-c ...
- Jenkins与.NET项目
转自: https://blog.dangl.me/categories Continuous Integration RSS Date Post 2016-10-20 Set Up Private ...
- jenkins2 插件安装
文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd Jenkins的安装包和插件在7个国家有20多个镜 ...
- APP API如何维护多个版本的一些想法?
1.第一种形式:api版本号放在url路径中 https://api.example.com/v1/user/ID https://api.example.com/v2/user/ID https:/ ...
- C#与数据库访问技术总结(十二)数据阅读器(DataReader)2
遍历数据阅读器中的记录 当ExecuteReader方法返回DataReader对象时,当前光标的位置在第一条记录的前面. 必须调用阅读器的Read方法把光标移动到第一条记录,然后,第一条记录将变成当 ...
- 仿IOS 开关按钮
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...