POJ 3294不小于k个字符串中的最长子串
不小于k个字符串中的最长子串
Description
You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.
The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant's life forms ended up with a large fragment of common DNA.
Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.
Input
Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.
Output
For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.
Sample Input
3 abcdefg bcdefgh cdefghi 3 xxx yyy zzz 0
Sample Output
bcdefg cdefgh ?
Source
Sol
考虑广义SAM
维护出每一个点出现次数。
暴力把所有可能的点拿出来,二分hash sort就好了

#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #define maxn 2000006 #define ll unsigned long long #define P 793999 using namespace std; int n,rt,la,cnt,len[105]; char ch[105][5005]; int num[maxn],now[maxn]; struct sam{ int Max,par,nex[26]; int id,x; }s[maxn]; struct node{ int len; char ch[5005];ll h[5005]; void H(){ for(int j=1;j<=len;j++)h[j]=h[j-1]*P+ch[j]; } }ans[10005]; bool operator ==(node A,node B){ if(A.len!=B.len)return 0; return A.h[A.len]==B.h[B.len]; } int find(node A,node B){ int M=min(A.len,B.len); if(A.ch[1]!=B.ch[1])return 0; int l=1,r=M; while(l<r){ int mid=(l+r+1)>>1; if(A.h[mid]==B.h[mid])l=mid; else r=mid-1; } return l; } bool operator <(node A,node B){ int x=find(A,B); if(min(A.len,B.len)==x)return A.len<B.len; return A.ch[x+1]<B.ch[x+1]; } void init(){ for(int i=1;i<=cnt;i++){ for(int j=0;j<26;j++)s[i].nex[j]=0; s[i].Max=s[i].par=0; num[i]=now[i]=0; s[i].id=s[i].x=0; } rt=la=cnt=1; } void ins(int c,int x){ int np=++cnt,p=la;la=np;s[np].Max=s[p].Max+1; s[np].id=s[np].Max;s[np].x=x; for(;p&&!s[p].nex[c];p=s[p].par)s[p].nex[c]=np; if(!p)s[np].par=rt; else { int q=s[p].nex[c],nq; if(s[q].Max==s[p].Max+1)s[np].par=q; else { nq=++cnt;s[nq].Max=s[p].Max+1; s[nq].id=s[np].id;s[nq].x=x; for(int j=0;j<26;j++)s[nq].nex[j]=s[q].nex[j]; s[nq].par=s[q].par;s[q].par=s[np].par=nq; for(;p&&s[p].nex[c]==q;p=s[p].par)s[p].nex[c]=nq; } } } int main(){ while(~scanf("%d",&n)){ if(!n)break; init(); for(int i=1;i<=n;i++){ scanf(" %s",ch[i]+1); len[i]=strlen(ch[i]+1); la=rt; for(int j=1;j<=len[i];j++)ins(ch[i][j]-'a',i); cout<<len[i]<<endl; } for(int i=1;i<=n;i++){ int k=rt; for(int j=1;j<=len[i];j++){ k=s[k].nex[ch[i][j]-'a']; int p=k; for(;now[p]!=i&&p;p=s[p].par)now[p]=i,num[p]++; } } int Max=0; for(int i=1;i<=cnt;i++){ if(num[i]*2>n)Max=max(Max,s[i].Max); } int tp=0; for(int i=2;i<=cnt;i++){ if(s[i].Max==Max&&num[i]*2>n){ int l=0;tp++; for(int j=s[i].id-Max+1;j<=s[i].id;j++){ ans[tp].ch[++l]=ch[s[i].x][j]; } ans[tp].len=l;ans[tp].H(); } } if(!tp)puts("?"); else { sort(ans+1,ans+tp+1); for(int i=1;i<=tp;i++){ if(ans[i]==ans[i-1])continue; for(int j=1;j<=ans[i].len;j++)printf("%c",ans[i].ch[j]);puts(""); } } puts(""); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构