poj3294 出现次数大于n/2 的公共子串
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 13063 | Accepted: 3670 |
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
cdefgh
?
/*
* Author: sweat123
* Created Time: 2016/7/10 22:01:18
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
char s[][];
int n,m,wa[MAXN],wb[MAXN],wc[MAXN],r[MAXN],height[MAXN],Rank[MAXN],sa[MAXN];
int a[],cnt,vis[];
void da(int *r,int *sa,int n,int m){
int *x = wa,*y = wb;
for(int i = ; i < m; i++)wc[i] = ;
for(int i = ; i < n; i++)wc[x[i] = r[i]] ++;
for(int i = ; i < m; i++)wc[i] += wc[i-];
for(int i = n - ; i >= ; i--)sa[--wc[x[i]]] = i;
for(int p = ,k = ; p < n; k <<= , m = p){
p = ;
for(int i = n - k; i < n; i++)y[p++] = i;
for(int i = ; i < n; i++)if(sa[i] >= k)y[p++] = sa[i] - k;
for(int i = ; i < m; i++)wc[i] = ;
for(int i = ; i < n; i++)wc[x[y[i]]] ++;
for(int i = ; i < m; i++)wc[i] += wc[i-];
for(int i = n - ; i >= ; i--)sa[--wc[x[y[i]]]] = y[i];
swap(x,y);
p = ;
x[sa[]] = ;
for(int i = ; i < n; i++){
x[sa[i]] = (y[sa[i]] == y[sa[i-]] && y[sa[i]+k] == y[sa[i-]+k])?p-:p++;
}
}
}
void calheight(int *r,int *sa,int n){
for(int i = ; i <= n; i++)Rank[sa[i]] = i;
int j,k;
k = ;
for(int i = ; i < n; height[Rank[i++]] = k){
for(k?k--:,j = sa[Rank[i] - ]; r[i+k] == r[j+k]; k++);
}
}
int find(int x){
int l,r,m,ans;
l = ,r = cnt - ;
while(l <= r){
m = (l + r) >> ;
if(a[m] <= x){
ans = m;
l = m + ;
} else {
r = m - ;
}
}
return ans;
}
vector<int>q;
int ok(int x,int num){
int ans = ;
q.clear();
memset(vis,,sizeof(vis));
for(int i = ; i <= n; i++){
if(height[i] >= x){
int tp1 = find(sa[i-]);
int tp2 = find(sa[i]);
if(tp1 == tp2)continue;
//cout<<x<<' '<<height[i]<<' '<<tp1<<' '<<tp2<<' '<<sa[i]<<' '<<sa[i-1]<<' ';
//cout<<ans<<' '<<vis[tp1]<<' '<<vis[tp2]<<endl;
if(tp1 < || tp2 < )cout<<<<endl;
if(!vis[tp1]){
ans += ;
vis[tp1] = ;
}
if(!vis[tp2]){
ans += ;
vis[tp2] = ;
}
} else {
if(ans > num / ){
q.push_back(sa[i-]);
ans = ;
memset(vis,,sizeof(vis));
} else{
ans = ;
memset(vis,,sizeof(vis));
}
}
}
if(ans > num / ){
q.push_back(sa[n]);
}
if(q.size() > )return ;
return ;
}
void solve(int num){
memset(vis,,sizeof(vis));
int fl,fr,m,ans = -;
fl = ,fr = n;
while(fl <= fr){
m = (fl + fr) >> ;
if(ok(m,num)){
ans = m;
fl = m + ;
} else {
fr = m - ;
}
}
if(ans <= ){
printf("?\n");
return ;
}
ok(ans,num);
for(int i = ; i < q.size(); i++){
for(int j = ; j < ans; j++){
printf("%c",r[q[i] + j]);
}
printf("\n");
}
}
int main(){
while(~scanf("%d",&m)){
if(!m)break;
memset(a,,sizeof(a));
memset(sa,,sizeof(sa));
cnt = ;
a[] = -;
for(int i = ; i <= m; i++){
scanf("%s",s[i]);
}
n = ;
for(int i = ; i <= m; i++){
int len = strlen(s[i]);
for(int j = ; j < len; j++){
r[n++] = s[i][j];
}
r[n++] = + i;
a[cnt] = a[cnt - ] + len + ;
cnt += ;
}
n -= ;
r[n] = ;
da(r,sa,n+,);
calheight(r,sa,n);
solve(m);
printf("\n");
}
return ;
}
poj3294 出现次数大于n/2 的公共子串的更多相关文章
- POJ3294 Life Forms —— 后缀数组 最长公共子串
题目链接:https://vjudge.net/problem/POJ-3294 Life Forms Time Limit: 5000MS Memory Limit: 65536K Total ...
- 笔试算法题(30):从已排序数组中确定数字出现的次数 & 最大公共子串和最大公共序列(LCS)
出题:在已经排序的数组中,找出给定数字出现的次数: 分析: 解法1:由于数组已经排序,所以可以考虑使用二分查找确定给定数字A的第一个出现的位置m和最后一个出现的位置n,最后m-n+1就是A出现的次数: ...
- 查找出现次数大于n/k的重复元素
本文是对一篇英文论文的总结:Finding Repeated Elements.想看原文,请Google之. 这个问题的简单形式是“查找出现次数大于n/2的重复元素”.我们先从简单问题开始,然后再做扩 ...
- [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- oracle 查出一个表中字段值出现次数大于2的所有记录
表web_order 列 name ,businesscode, a.account 周桥 18929609222 3754031157710000妙药 18929609233 3754031157 ...
- [算法]在数组中找到出现次数大于N/K的数
题目: 1.给定一个整型数组,打印其中出现次数大于一半的数.如果没有出现这样的数,打印提示信息. 如:1,2,1输出1. 1,2,3输出no such number. 2.给定一个整型数组,再给 ...
- 在数组中寻找出现次数大于N/K的数
给定一个int[]数组,给定一个整数k,打印所有出现次数大于N/k的数,没有的话,给出提示信息. === 核心思想:一次在数组中删除K个不同的数,不停的删除,直到剩下的数的种类不足K就停止删除,那么如 ...
- 《程序员代码面试指南》第八章 数组和矩阵问题 在数组中找到出现次数大于N/K 的数
题目 在数组中找到出现次数大于N/K 的数 java代码 package com.lizhouwei.chapter8; import java.util.ArrayList; import java ...
- CodeForces - 840D:(主席树求出现区间出现次数大于某值的最小数)
Once, Leha found in the left pocket an array consisting of n integers, and in the right pocket q que ...
随机推荐
- GitHub 下载文件夹
工具 TortoiseSVN 步骤 1.打开要下载的项目,选中要下载的文件夹,右键 选择 复制链接地址 2.把链接中的 tree/master 改成 trunk ,(trunk是master分支,可以 ...
- ASP.NET MVC
ASP.NET MVC 就是根据 Model 2 模式设计的.对于 HTTP 请求的拦截以实现对目标 Controller 和 Action 的解析是通过一个自定义 Http Module 来实现的, ...
- python pip install mysql-connector-python
sudo pip install mysql-connector-python 报错信息:Collecting mysql-connector-python Could not find a vers ...
- linux内核启动以及文件系统的加载过程
Linux 内核启动及文件系统加载过程 当u-boot 开始执行 bootcmd 命令,就进入 Linux 内核启动阶段.普通 Linux 内核的启动过程也可以分为两个阶段.本文以项目中使用的 lin ...
- .net(c#)提取多层嵌套的JSON
Newtonsoft.Json.Net20.dll 下载请访问http://files.cnblogs.com/hualei/Newtonsoft.Json.Net20.rar 在.net 2.0中提 ...
- 机器学习实战--logistic回归
#encoding:utf-8 from numpy import * def loadDataSet(): #加载数据 dataMat = []; labelMat = [] fr = open(' ...
- HTML 学习笔记 CSS(列表)
CSS列表属性允许你放置 改变列表项标志 或者将图像作为列表项标志. CSS列表 从某中意义上讲 不是描述性的文本的任何内容都可以认为是列表.人口普查.太阳系.家谱.参观菜单,甚至你的所有朋友都可以表 ...
- HTML 学习笔记(块 和 类)
HTML <div> 和 <span> 可以通过<div>和<span>将HTML元素组合起来. HTML块元素 大多数HTML元素被定义为块级元素或者 ...
- 在表单中元素的onchange事件的兼容性问题
onchange:在值发生改变的时候触发 text:当光标离开的时候如果内容有变化就触发 radio/check:标准浏览器下点击的时候只要值变了就触发 非标准浏览器下焦点离开的时候如果值变了就触发
- 泛型中? super T和? extends T的区别
原文出处: 并发编程网 经常发现有List<? super T>.Set<? extends T>的声明,是什么意思呢?<? super T>表示包括T在内的任何T ...