poj3294 出现次数大于n/2 的公共子串

本文介绍了一种寻找多个字符串中最长公共子串的算法,并详细解释了其实现过程。通过构建特殊的字符串数组并运用后缀数组等数据结构,该算法能够高效地找到出现频率超过一半的公共子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Life Forms
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

bcdefg
cdefgh

?
 
题意:
有n个字符串,求出现n/2以上1次数的公共子串。
 
思路:
将所有的子串连起来,中间用没有出现过的符号隔开(不过要注意的是,不能通过s[i] + 某个值来处理,因为ascill码中知道127,在加的话会先加成字符,由于大于127可能会乱码,这样其实给r[]的时候,值是不对的。
还有就是我写的时候,从1开始加竟然有问题,也不清楚为什么。)。然后二分长度(也就是答案),枚举判断复合长度的后缀,因为每一个字符串只能提供一个子串,所以我们要判断这个子串在哪一个字符串中(换一句话说 在每一组height中 每个字符串只能给你一个子串)。最后存一下答案就ok了。
 
/*
 * 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 = 201000;
char s[220][1010];
int n,m,wa[MAXN],wb[MAXN],wc[MAXN],r[MAXN],height[MAXN],Rank[MAXN],sa[MAXN];
int a[250],cnt,vis[250];
void da(int *r,int *sa,int n,int m){
    int *x = wa,*y = wb;
    for(int i = 0; i < m; i++)wc[i] = 0;
    for(int i = 0; i < n; i++)wc[x[i] = r[i]] ++;
    for(int i = 0; i < m; i++)wc[i] += wc[i-1];
    for(int i = n - 1; i >= 0; i--)sa[--wc[x[i]]] = i;
    for(int p = 1,k = 1; p < n; k <<= 1, m = p){
        p = 0;
        for(int i = n - k; i < n; i++)y[p++] = i;
        for(int i = 0; i < n; i++)if(sa[i] >= k)y[p++] = sa[i] - k;
        for(int i = 0; i < m; i++)wc[i] = 0;
        for(int i = 0; i < n; i++)wc[x[y[i]]] ++;
        for(int i = 0; i < m; i++)wc[i] += wc[i-1];
        for(int i = n - 1; i >= 0; i--)sa[--wc[x[y[i]]]] = y[i];
        swap(x,y);
        p = 1;
        x[sa[0]] = 0;
        for(int i = 1; i < n; i++){
            x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k])?p-1:p++;
        }   
    }   
}
void calheight(int *r,int *sa,int n){
    for(int i = 1; i <= n; i++)Rank[sa[i]] = i;
    int j,k;
    k = 0;
    for(int i = 0; i < n; height[Rank[i++]] = k){
        for(k?k--:0,j = sa[Rank[i] - 1]; r[i+k] == r[j+k]; k++);
    }   
}
int find(int x){
    int l,r,m,ans;
    l = 0,r = cnt - 1;
    while(l <= r){
        m = (l + r) >> 1;
        if(a[m] <= x){
            ans = m;
            l = m + 1;
        } else {
            r = m - 1;   
        }
    }
    return ans;
}
vector<int>q;
int ok(int x,int num){
    int ans = 0;
    q.clear();
    memset(vis,0,sizeof(vis));
    for(int i = 1; i <= n; i++){
        if(height[i] >= x){
            int tp1 = find(sa[i-1]);
            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 < 0 || tp2 < 0)cout<<1<<endl;
            if(!vis[tp1]){
                ans += 1;
                vis[tp1] = 1;   
            } 
            if(!vis[tp2]){
                ans += 1;
                vis[tp2] = 1;   
            }
        } else {
            if(ans > num / 2){
                q.push_back(sa[i-1]);
                ans = 0;
                memset(vis,0,sizeof(vis));
            } else{
                ans = 0;
                memset(vis,0,sizeof(vis));   
            }
        }
    }     
    if(ans > num / 2){
        q.push_back(sa[n]);
    }
    if(q.size() > 0)return 1;
    return 0;
}
void solve(int num){
    memset(vis,0,sizeof(vis));
    int fl,fr,m,ans = -1;
    fl = 0,fr = n;
    while(fl <= fr){
        m = (fl + fr) >> 1;
        if(ok(m,num)){
            ans = m;
            fl = m + 1;
        } else {
            fr = m - 1;
        }
    }
    if(ans <= 0){
        printf("?\n");
        return ;       
    }
    ok(ans,num);
    for(int i = 0; i < q.size(); i++){
        for(int j = 0; j < ans; j++){
            printf("%c",r[q[i] + j]);   
        }
        printf("\n");
    }
}
int main(){
    while(~scanf("%d",&m)){
        if(!m)break;
        memset(a,0,sizeof(a));
        memset(sa,0,sizeof(sa)); 
        cnt = 1;
        a[0] = -1;
        for(int i = 1; i <= m; i++){
            scanf("%s",s[i]);
        }
        n = 0;
        for(int i = 1; i <= m; i++){
            int len = strlen(s[i]);
            for(int j = 0; j < len; j++){
                r[n++] = s[i][j];
            } 
            r[n++] = 150 + i;
            a[cnt] = a[cnt - 1] + len + 1;
            cnt += 1;
        }
        n -= 1;
        r[n] = 0;
        da(r,sa,n+1,300);
        calheight(r,sa,n);
        solve(m);
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/sweat123/p/5659408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值