后缀数组LCP + 二分 - UVa 11107 Life Forms
Life Forms #
Problem's Link
#
Mean:
给你n个串,让你找出出现次数大于n/2的最长公共子串。如果有多个,按字典序排列输出。
analyse:
经典题。
直接二分判断答案。
判断答案p时,我们扫一遍height数组,如果height[i]<p时开辟一个新段。
判断时用set存储所在串编号,不仅起到去重的作用,而且也起到统计段长的作用。
也可以直接用字符串hash来做,也是先二分,然后O(n)判断,时间复杂度和后缀数组一样。
Time complexity: O(N*logN)
Source code:
1.后缀数组:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-09-05-14.44
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
const int MAXLEN = 200005;
struct Suffix
{
int s[MAXLEN];
int sa[MAXLEN], t[MAXLEN], t2[MAXLEN], c[MAXLEN], n;
int Rank[MAXLEN], height[MAXLEN];
void build_sa(int m)
{
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 i, j, k = 0;
for(i = 0; i < n; i++) Rank[sa[i]] = i;
for(i = 0; i < n; i++)
{
if(k) k--;
if(Rank[i] == 0) continue;
int j = sa[Rank[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[Rank[i]] = k;
}
}
} Suf;
const int N = 1005;
int n, l, r, id[MAXLEN];
char str[N];
bool judge(int x, int bo)
{
set<int> vis;
vis.insert(id[Suf.sa[1]]);
for(int i = 2; i < Suf.n; i++)
{
while(i < Suf.n && Suf.height[i] >= x)
{
vis.insert(id[Suf.sa[i]]);
i++;
}
if(vis.size() * 2 > n)
{
if(bo == 0)
return true;
for(int j = 0; j < x; j++)
printf("%c", Suf.s[Suf.sa[i - 1] + j]);
printf("\n");
}
vis.clear();
vis.insert(id[Suf.sa[i]]);
}
return false;
}
void solve()
{
if(!judge(1, 0))
{
printf("?\n");
return;
}
l = 1; r++;
while(l < r)
{
int mid = (l + r) / 2;
if(judge(mid, 0)) l = mid + 1;
else r = mid;
}
l--;
judge(l, 1);
}
int main()
{
int bo = 0;
while(~scanf("%d", &n) && n)
{
if(bo) printf("\n");
else bo = 1;
if(n == 1)
{
scanf("%s", str);
printf("%s\n", str);
continue;
}
int tot = 0;
r = 0;
for(int i = 0; i < n; i++)
{
scanf("%s", str);
int len = strlen(str);
r = max(len, r);
for(int j = 0; j < len; j++)
{
id[tot] = i;
Suf.s[tot++] = str[j];
}
id[tot] = i;
Suf.s[tot++] = 'z' + i + 1;
}
Suf.n = tot;
Suf.build_sa('z' + n + 1);
Suf.getHeight();
solve();
}
return 0;
}
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-09-05-14.44
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
const int MAXLEN = 200005;
struct Suffix
{
int s[MAXLEN];
int sa[MAXLEN], t[MAXLEN], t2[MAXLEN], c[MAXLEN], n;
int Rank[MAXLEN], height[MAXLEN];
void build_sa(int m)
{
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 i, j, k = 0;
for(i = 0; i < n; i++) Rank[sa[i]] = i;
for(i = 0; i < n; i++)
{
if(k) k--;
if(Rank[i] == 0) continue;
int j = sa[Rank[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[Rank[i]] = k;
}
}
} Suf;
const int N = 1005;
int n, l, r, id[MAXLEN];
char str[N];
bool judge(int x, int bo)
{
set<int> vis;
vis.insert(id[Suf.sa[1]]);
for(int i = 2; i < Suf.n; i++)
{
while(i < Suf.n && Suf.height[i] >= x)
{
vis.insert(id[Suf.sa[i]]);
i++;
}
if(vis.size() * 2 > n)
{
if(bo == 0)
return true;
for(int j = 0; j < x; j++)
printf("%c", Suf.s[Suf.sa[i - 1] + j]);
printf("\n");
}
vis.clear();
vis.insert(id[Suf.sa[i]]);
}
return false;
}
void solve()
{
if(!judge(1, 0))
{
printf("?\n");
return;
}
l = 1; r++;
while(l < r)
{
int mid = (l + r) / 2;
if(judge(mid, 0)) l = mid + 1;
else r = mid;
}
l--;
judge(l, 1);
}
int main()
{
int bo = 0;
while(~scanf("%d", &n) && n)
{
if(bo) printf("\n");
else bo = 1;
if(n == 1)
{
scanf("%s", str);
printf("%s\n", str);
continue;
}
int tot = 0;
r = 0;
for(int i = 0; i < n; i++)
{
scanf("%s", str);
int len = strlen(str);
r = max(len, r);
for(int j = 0; j < len; j++)
{
id[tot] = i;
Suf.s[tot++] = str[j];
}
id[tot] = i;
Suf.s[tot++] = 'z' + i + 1;
}
Suf.n = tot;
Suf.build_sa('z' + n + 1);
Suf.getHeight();
solve();
}
return 0;
}
2.字符串hash:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-09-03-12.21
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
const int MAXN=10001010,seed=173;
int n,id[MAXN];
char s[MAXN],str[5010];
vector<pair<int,int> >same;
int getBegin(int st,int en)
{
for(int i=0; i<same.size(); ++i)
if(st>=((&same[i])->first) && en<=((&same[i])->second))
return (&same[i])->first;
return -1;
}
vector<string> ans;
bool judge(int p,bool ty)
{
vector<pair<ULL,pair<int,int> > >v;
ULL hv=0,base=1;
for(int i=0; i<p; ++i)
{
hv=hv*seed+s[i];
base*=seed;
}
v.push_back(make_pair(hv,make_pair(0,p-1)));
int len=strlen(s);
for(int i=p; i<len; ++i)
{
hv=hv*seed+s[i] - base*s[i-p];
if(getBegin(i-p+1,i)!=-1)
v.push_back(make_pair(hv,make_pair(i-p+1,i)));
}
sort(v.begin(),v.end());
set<int> id;
int st,en,cnt;
bool f=false;
ans.clear();
auto p1=v.begin(),p2=v.begin();
++p2;
for(; p2!=v.end(); ++p1,++p2)
{
if(p2->first != p1->first) id.clear();
else
{
while(p2->first == p1->first)
{
st=(&p2->second)->first;
en=(&p2->second)->second;
if(getBegin(st,en)!=-1&&
getBegin((&p1->second)->first,(&p1->second)->second)!=-1&&
id.find(getBegin(st,en))==id.end() &&
getBegin((&p1->second)->first,(&p1->second)->second)!=getBegin(st,en))
{
id.insert(getBegin(st,en));
}
++p1,++p2;
}
--p1,--p2;
if((id.size()+1)*2>n)
{
f=true;
st=(&p2->second)->first;
en=(&p2->second)->second;
cnt=0;
for(int i=st; i<=en; ++i)
str[cnt++]=s[i];
str[cnt]='\0';
ans.push_back(str);
id.clear();
}
}
}
if(!ty) return f;
sort(ans.begin(),ans.end());
for(int i=0; i<ans.size(); ++i)
printf("%s\n",ans[i].c_str());
}
int main()
{
int Cas=0;
while(~scanf("%d",&n) && n)
{
if(Cas++) puts("");
if(n==1)
{
scanf("%s",s);
puts(s);
continue;
}
same.clear();
int maxLen=0,st,en;
memset(s,0,sizeof s);
for(int i=0; i<n; ++i)
{
scanf("%s",str);
st=strlen(s);
maxLen=max(maxLen,(int)strlen(str));
strcat(s,str);
en=strlen(s);
same.push_back(make_pair(st,en-1));
}
if(!judge(1,0))
{
puts("?");
continue;
}
int l=1,r=maxLen,mid;
while(r>l+1)
{
mid=l+(r-l)/2;
if(judge(mid,0)) l=mid;
else r=mid;
}
judge(l,1);
}
return 0;
}
/*
*/
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-09-03-12.21
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
const int MAXN=10001010,seed=173;
int n,id[MAXN];
char s[MAXN],str[5010];
vector<pair<int,int> >same;
int getBegin(int st,int en)
{
for(int i=0; i<same.size(); ++i)
if(st>=((&same[i])->first) && en<=((&same[i])->second))
return (&same[i])->first;
return -1;
}
vector<string> ans;
bool judge(int p,bool ty)
{
vector<pair<ULL,pair<int,int> > >v;
ULL hv=0,base=1;
for(int i=0; i<p; ++i)
{
hv=hv*seed+s[i];
base*=seed;
}
v.push_back(make_pair(hv,make_pair(0,p-1)));
int len=strlen(s);
for(int i=p; i<len; ++i)
{
hv=hv*seed+s[i] - base*s[i-p];
if(getBegin(i-p+1,i)!=-1)
v.push_back(make_pair(hv,make_pair(i-p+1,i)));
}
sort(v.begin(),v.end());
set<int> id;
int st,en,cnt;
bool f=false;
ans.clear();
auto p1=v.begin(),p2=v.begin();
++p2;
for(; p2!=v.end(); ++p1,++p2)
{
if(p2->first != p1->first) id.clear();
else
{
while(p2->first == p1->first)
{
st=(&p2->second)->first;
en=(&p2->second)->second;
if(getBegin(st,en)!=-1&&
getBegin((&p1->second)->first,(&p1->second)->second)!=-1&&
id.find(getBegin(st,en))==id.end() &&
getBegin((&p1->second)->first,(&p1->second)->second)!=getBegin(st,en))
{
id.insert(getBegin(st,en));
}
++p1,++p2;
}
--p1,--p2;
if((id.size()+1)*2>n)
{
f=true;
st=(&p2->second)->first;
en=(&p2->second)->second;
cnt=0;
for(int i=st; i<=en; ++i)
str[cnt++]=s[i];
str[cnt]='\0';
ans.push_back(str);
id.clear();
}
}
}
if(!ty) return f;
sort(ans.begin(),ans.end());
for(int i=0; i<ans.size(); ++i)
printf("%s\n",ans[i].c_str());
}
int main()
{
int Cas=0;
while(~scanf("%d",&n) && n)
{
if(Cas++) puts("");
if(n==1)
{
scanf("%s",s);
puts(s);
continue;
}
same.clear();
int maxLen=0,st,en;
memset(s,0,sizeof s);
for(int i=0; i<n; ++i)
{
scanf("%s",str);
st=strlen(s);
maxLen=max(maxLen,(int)strlen(str));
strcat(s,str);
en=strlen(s);
same.push_back(make_pair(st,en-1));
}
if(!judge(1,0))
{
puts("?");
continue;
}
int l=1,r=maxLen,mid;
while(r>l+1)
{
mid=l+(r-l)/2;
if(judge(mid,0)) l=mid;
else r=mid;
}
judge(l,1);
}
return 0;
}
/*
*/
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/4779703.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2014-09-02 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
2014-09-02 数论 - 算数基本定理的运用 --- nefu 118 : n!后面有多少个0
2014-09-02 数论 - 素数的运用 --- poj 2689 : Prime Distance