Life Forms

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 ?

思路

  • 后缀数组
  • 由于答案子串长度和答案个数具有单调性,可用二分答案法
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; const int Max=501;
const int MAX=1e5+1500;
string s,ss[Max];
int n,mx;
int rnk[MAX],sa[MAX];
int tmp[MAX],c[MAX];
int h[MAX],sy[MAX]; void lcp()
{
h[0]=0;
for(int i=0,j=rnk[0],k=0; i<n-1; i++,k++)
while(k>=0&&s[i]!=s[sa[j-1]+k])
h[j]=k--,j=rnk[sa[j]+1];
} void sarank()
{
int na=256;
memset(c,0,na*sizeof(int));
n=s.size();
s[n]=1;n++;
for(int i=0; i<n; i++) rnk[i]=(int)s[i],c[rnk[i]]++;
for(int i=1; i<na; i++) c[i]=c[i]+c[i-1];
for(int i=0; i<n; i++) c[rnk[i]]--,sa[c[rnk[i]]]=i;
int j;
for(int len=1; len<n; len=len<<1)
{
for(int i=0; i<n; i++)
{
j=sa[i]-len;
if(j<0) j=j+n;
tmp[c[rnk[j]]++]=j;
}
sa[tmp[c[0]=0]]=j=0;
for(int i=1; i<n; i++)
{
if(rnk[tmp[i]]!=rnk[tmp[i-1]]||rnk[tmp[i]+len]!=rnk[tmp[i-1]+len]) c[++j]=i;
sa[tmp[i]]=j;
}
memcpy(rnk,sa,n*sizeof(int));
memcpy(sa,tmp,n*sizeof(int));
if(j>=n-1) break;
}
} int T;
bool fl[Max]; void print(int ans)
{
int tot=0,i=0;
memset(fl,false,sizeof(fl));
while(i<n)
{
tot=0;
if(h[i]>=ans)
{
while(h[i]>=ans)
{
if(!fl[sy[sa[i]]]&&sy[sa[i]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i]]]=true;
if(!fl[sy[sa[i-1]]]&&sy[sa[i-1]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i-1]]]=true;
i++;
}
if(tot>T/2)
{
for(int j=sa[i-1]; j<sa[i-1]+ans; j++)
cout<<s[j];
cout<<endl;
}
memset(fl,false,sizeof(fl));
}
i++;
}
} bool pd(int m)
{
int tot=0,i=1;bool f;
memset(fl,false,sizeof(fl));
while(i<n)
{
tot=0;f=false;
if(h[i]>=m)
{
while(h[i]>=m)
{
if(h[i]==m) f=true;
if(!fl[sy[sa[i]]]&&sy[sa[i]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i]]]=true;
if(!fl[sy[sa[i-1]]]&&sy[sa[i-1]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i-1]]]=true;
i++;
if(tot>T/2&&f) return true;
}
memset(fl,false,sizeof(fl));
}
i++;
}
return false;
} void solve()
{
int l=1,r=mx,mid,ans=0;
while(l<=r)
{
mid=(l+r)>>1;
if(pd(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
if(ans) print(ans);
else printf("?\n");
} int main()
{
int sl;
bool flag=true;
while(true)
{
if(!flag) printf("\n");
else flag = false;
scanf("%d",&T);
if(T==0) break;
s="";mx=0;
for(int i=0; i<Max; i++) c[i]=h[i]=sa[i]=sy[i]=tmp[i]=rnk[i]=0;
for(int i=1; i<=T; i++)
{
cin>>ss[i];sl=ss[i].size();
for(int j=s.size(); j<s.size()+sl; j++) sy[j]=i;
s=s+ss[i]+char(i);
mx=max(mx,sl);
}
sarank(),lcp();
solve();
}
return 0;
}

Life Forms[poj3294]题解的更多相关文章

  1. 后缀数组练习4:Life Forms

    有一个细节不是特别懂,然后的话细节有点多,就是挺难发现的那一种,感谢大佬的博客 1470: 后缀数组4:Life Forms poj3294 时间限制: 1 Sec  内存限制: 128 MB提交: ...

  2. POJ3294 Life Forms —— 后缀数组 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-3294 Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total ...

  3. 【POJ3294】 Life Forms (后缀数组+二分)

    Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, d ...

  4. poj3294 --Life Forms

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12483   Accepted: 3501 Descr ...

  5. Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)

    (累了,这题做了很久!) Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8683   Accepted ...

  6. 【poj3294】 Life Forms

    http://poj.org/problem?id=3294 (题目链接) 题意 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. Solution 后缀数组论文题.. 将 n 个字符串 ...

  7. 【POJ3294】Life Forms(后缀数组,二分)

    题意: n<=100 len[i]<=1000 思路:这是一道论文题 ..]of longint; ch:..]of ansistring; n,n1,l,r,mid,last,i,j,m ...

  8. POJ3294 Life Forms(后缀数组)

    引用罗穗骞论文中的话: 将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,用和例3 同样的方法将后缀分成若干组,判断每组的后缀是否出现在不小于k 个的原串中 ...

  9. poj3294 Life Forms(后缀数组)

    [题目链接] http://poj.org/problem?id=3294 [题意] 多个字符串求出现超过R次的最长公共子串. [思路] 二分+划分height,判定一个组中是否包含不小于R个不同字符 ...

随机推荐

  1. KVM管理工具webvirtmgr的使用

    WebVirtMgr的日常配置:添加宿主机,创建虚拟机,磁盘扩容,快照等具体操作记录如下: 一.创建虚拟机 1.创建存储池 点击创建的宿主机,进入虚拟机部署界面 点击“存储池”按钮,创建存储池(即创建 ...

  2. session和cookie的最深刻理解

    先说session 对SESSION的争论好象一直没有停止过,不过幺麽能理解SESSION的人应该占90以上.但还是讲讲,别嫌老~ 有一些人赞成用SESSION,有一些人不赞成.但这个问题到底要怎么说 ...

  3. codewars--js--vowels counting+js正则相关知识

    问题描述: Return the number (count) of vowels in the given string. We will consider a, e, i, o, and u as ...

  4. git系列之---工作中项目的常用git操作

    0.本地git的安装 官网下载 1.git 配置 git config user.name  查看 用户名 git config user.email   查看 邮箱 git config --glo ...

  5. Caliburn.Micro框架之Action Convertions

    首先新建一个项目,名称叫Caliburn.Micro.ActionConvertions 然后删掉MainWindow.xaml 然后去app.xaml删掉StartupUri这行代码 其次,安装Ca ...

  6. 1Python学习CentOS 7 Linux环境搭建

    鉴于python3目前已成流行之势,而各发行版Linux依然是自带python2.x,笔者尝试在centos7下,部署Python3.x与2.x共存环境 本文参考博主良哥95网址https://blo ...

  7. CentOS7安装MySQL报错,解决Failed to start mysqld.service: Unit not found

    当输入命令 ~]# systemctl start mysql.service 要启动MySQL数据库是却是这样的提示 Failed to start mysqld.service: Unit not ...

  8. P1843 奶牛晒衣服

    链接:Miku -------------------------------- 这是一道二分答案的题,我们要二分时间. 对于每件衣服,我们自然是能让它自己蒸发就自己蒸发,这样才是最优的. 那么我闷可 ...

  9. Kafka消费者没有收到通知的分析

    今天遇到两位三方人员跟我反馈,某微服务的异步接口功能不正常了,由于该异步接口采用Kafka异步消息的方案,对方说没有收到Kafka给消费者的通知,根据此问题,联系了相关人员进行了分析: (一)明确环境 ...

  10. 【48】数据扩充(Data augmentation)

    数据扩充(Data augmentation) 大部分的计算机视觉任务使用很多的数据,所以数据扩充是经常使用的一种技巧来提高计算机视觉系统的表现.我认为计算机视觉是一个相当复杂的工作,你需要输入图像的 ...