UVA 11107 Life Forms 后缀数组
来源:互联网 发布:淘宝无线链接 编辑:程序博客网 时间:2024/05/19 12:39
白书上的例题,贴的模板。。。
[Submit] [Go Back] [Status]
Description
Problem C: Life Forms
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.
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.
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
3abcdefgbcdefghcdefghi3xxxyyyzzz0
Output for Sample Input
bcdefgcdefgh?
Gordon V. Cormack
[Submit] [Go Back] [Status]
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(int i=0;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define DWN(i,h,l) for(int i=(h);i>=(l);--i)#define CLR(vis) memset(vis,0,sizeof(vis))#define MST(vis,pos) memset(vis,pos,sizeof(vis))#define MAX3(a,b,c) max(a,max(b,c))#define MAX4(a,b,c,d) max(max(a,b),max(c,d))#define MIN3(a,b,c) min(a,min(b,c))#define MIN4(a,b,c,d) min(min(a,b),min(c,d))#define PI acos(-1.0)#define INF 0x7FFFFFFF#define LINF 1000000000000000000LL#define eps 1e-8typedef long long ll;const int maxn=100000+1000;int r[maxn] ;int sa[maxn], height[maxn] ,rank[maxn] , id[maxn] ;int wa[maxn] ,wb[maxn], ky[maxn], cn[maxn] ;inline bool cmp(int *r ,int a,int b,int l){ return r[a]==r[b] && r[a+l] == r[b+l] ;}void build_sa(int *r, int *sa ,int n , int m){ int i, k , p , *x=wa ,*y = wb; for(i=0; i<m; i++) cn[i] = 0; for(i=0; i<n; i++) cn[ x[i] = r[i] ] ++ ; for(i=1; i<m; i++) cn[i] += cn[i-1] ; for(i=n-1; i>=0; i--) sa[ --cn[ x[i] ] ] = i ; for(k=1,p=1; p<n; k<<=1 , m=p) { for(p=0,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<n; i++) ky[i] = x[ y[i] ] ; for(i=0; i<m; i++) cn[i] = 0; for(i=0; i<n; i++) cn[ky[i]]++ ; for(i=1; i<m; i++) cn[i] += cn[i-1] ; for(i=n-1; i>=0; i--) sa[ --cn[ky[i]] ] = y[i] ; for(swap(x ,y),p=1,x[sa[0]]=0,i=1; i<n; i++) x[sa[i]] = cmp(y,sa[i-1],sa[i],k) ? p-1 : p++ ; } return ;}void get_height(int *s, int n) { int i, j , k=0; for(i=0; i<n; i++) rank[sa[i]] = i ; for(i=0; i<n-1; i++) { if(k) k-- ; j = sa[rank[i]-1] ; while(s[i+k] == s[j+k]) k++ ; height[rank[i]] = k; }}bool check(int n ,int m, int mid){ set<int>vis ; vis.insert(id[sa[1]]) ; for(int i=2;i<n;i++) { while(i<n && height[i] >= mid) vis.insert(id[sa[i]]),i++ ; if(vis.size()*2>m) return true ; vis.clear(); vis.insert(id[sa[i]]) ; } return false ;}void print(int n,int m,int p){ set<int>vis ; vis.insert(id[sa[1]]); for(int i=2; i<n; i++) { while(i<n && height[i]>= p) vis.insert(id[sa[i]]),i++ ; if(vis.size()*2>m) { int a=sa[i-1] ; for(int j=0;j<p;j++) putchar(r[a+j]) ; puts("") ; } vis.clear() ; vis.insert(id[sa[i]]) ; }}int main(){ int m,flag=1; while(scanf("%d",&m)==1 && m) { if(!flag) puts(""); else flag=0; char str[1111]; int n=0; FOR(i,1,m) { CLR(str); scanf("%s",str); for(int j=0; str[j]; ++j) { id[n]=i; r[n++]=str[j]; } id[n]=i; r[n++]='z'+i; } if(m==1) { puts(str); continue; } id[n]=n+1; r[n++]=0; build_sa(r,sa,n,'z'+m+2); get_height(r,n); if(!check(n,m,1)) { puts("?"); continue; } int L=1,R=1000,mid; while(L<R) { mid=L+(R-L+1)/2; if(check(n,m,mid)) L=mid ; else R=mid-1; } print(n,m,L); } return 0;}
- UVA 11107(Life Forms-后缀数组+二分)
- UVA 11107 Life Forms 后缀数组
- UVA 11107 - Life Forms(后缀数组)
- uva 11107 - Life Forms(后缀数组)
- uva 11107 Life Forms(后缀数组)
- Uva 11107 Life Forms(后缀数组 + 二分)
- UVA 11107 Life Forms(后缀数组)
- UVA 11107 Life Forms (后缀数组 + 二份答案)
- UVA 11107 Life Forms(后缀数组 OR hash)
- UVA 11107 Life Forms(后缀数组+LCP)
- POJ 3294 (UVA 11107) Life Forms 后缀数组
- UVA 11107 Life Forms(后缀数组+二分)
- UVa 11107 - Life Forms (后缀数组 求出现K次的串 根据height分组)
- 连续子串中出现超过一半次数的字符串 后缀数组 uva 11107 Life Forms
- uva11107 - Life Forms 后缀数组
- POJ3294:Life Forms(后缀数组)
- 后缀数组 - poj3294 Life Forms
- poj3294 Life Forms - 后缀数组
- 关于WIN32 窗口信息的各种获取
- TortoiseSVN使用简介
- (转)百度2014移动研发笔试题目——1013清华版
- onMenuOpened不调用问题
- oracle中imp导入数据中文乱码问题
- UVA 11107 Life Forms 后缀数组
- 从入门到精通我是这样学习算法的
- Spring 多数据源事务配置问题
- Solr从Mongodb索引数据(上
- @ResponseBody配置返回
- 用synchronized、notify、wait实现一个火车票卖票程序
- vi下复制粘贴操作
- iOS---self与self class有什么用法上的区别
- 10:java集合框架