poj3294 后缀数组+二分

(http://www.elijahqi.win/2017/07/19/poj3294-%E5%90%8E%E7%BC%80%E6%95%B0%E7%BB%84%E4%BA%8C%E5%88%86/)
Life Forms
Time Limit: 5000MS

Memory Limit: 65536K
Total Submissions: 15713

Accepted: 4615
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
Waterloo Local Contest, 2006.9.30
给定n个字符串,求出现在不小于k/2个字符串中的最长子串。
将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组。(何琦后缀数组需要优化)
然后二分答案,将后缀分成若干组,判断每组的后缀是否出现在不小于k个的原串中。这个做法的时间复杂度为O(nlogn)。
二分答案的时候由于下一次check的时候有可能更新这一次亦有可能不更新这一次,所以我们另外加设一个数组储存,一开始时数组开小了RE 我以为时rank和queue冲突了,改掉queue 用了两个q,q1数组存储答案的起点,最后从起点输出ans长度即可
另外贡献一组数据
IN:
2
adsf
asdf
2
fdsa
df
5
abcd
bcde
cdef
defg
efgh
5
nshiyedashai
goanshab
woshidiyi
firstorloser
iamthefirst
4
acaaaaaaaaaaaaa
aaaaa
aa
a
4
aa
a
d
d
2
pabcp
pertp
0

OUT:
a
d
f
s

d
f

cd
de
ef

sh

aa

?

p
注意输出格式

#include<cstdio>
#include<cstring>
#define N 110000
char str1[1100];
int a[N],id[N];
int k,n,l,r,m,k1,count[N],rank[N<<1],rank1[N],tmp[N],sa[N],height[N],ans;
inline int max(int x,int y){
    return x>y?x:y;
}
int q[N],n2,q1[N],n3;
inline bool check(int x){
    bool visit[110],flag=false,flag1=false;int cnt=0;
    n2=0;
    for (int i=1;i<=n;++i){
        if (height[i]>=x){
            if (!visit[id[sa[i]]]) visit[id[sa[i]]]=true,cnt++;
        }else{
            memset(visit,false,sizeof(visit));
            visit[id[sa[i]]]=true;cnt=1;flag=false;
        }
        if ((cnt>(k1>>1))&&(!flag)) q[++n2]=sa[i],flag=true,flag1=true;
    }
    if (flag1) {
        for (int i=1;i<=n2;++i) q1[i]=q[i];n3=n2;
    }
    return flag1;
}
int main(){
    //freopen("poj3294.in","r",stdin);
    //freopen("poj3294.out","w",stdout);
    while(~scanf("%d",&k1),k1){
        n=0;m=30+k1;r=0;l=1;n2=0;
        for (int i=1;i<=k1;++i){
            scanf("%s",str1);
            int n1=strlen(str1);r=max(r,n1);
            for (int j=n+n1;j>n;--j) a[j]=str1[j-n-1]-'a'+1,id[j]=i;
            n=n+n1;
            a[++n]=30+i;id[n]=i;
        }
        //for (int i=1;i<=n;++i) printf("%d ",a[i]);printf("\n");
        for (int i=1;i<=m;++i) count[i]=0;
        memset(rank,0,sizeof(rank));
        for (int i=1;i<=n;++i) count[a[i]]=1;
        for (int i=1;i<=m;++i) count[i]+=count[i-1];
        for (int i=1;i<=n;++i) rank[i]=count[a[i]];
        k=0;
        for (int p=1;k!=n;p<<=1,m=k){
            for (int i=1;i<=m;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i+p]]++;
            for (int i=1;i<=m;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
            for (int i=1;i<=m;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i]]++;
            for (int i=1;i<=m;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
            memcpy(rank1,rank,sizeof(rank)>>1);
            rank[sa[1]]=k=1;
            for (int i=2;i<=n;++i){
                if (rank1[sa[i-1]]!=rank1[sa[i]]||rank1[sa[i-1]+p]!=rank1[sa[i]+p])++k;
                rank[sa[i]]=k;
            }
        }
        k=0;
        for (int i=1;i<=n;++i){
            if (rank[i]==0){
                height[1]=0;continue;
            }
            k=k==0?0:k-1;
            while (a[i+k]==a[sa[rank[i]-1]+k])++k; 
            height[rank[i]]=k;
        }
    //  for (int i=1;i<=n;++i) printf("%d ",height[i]);printf("\n");
        while (l<=r){
            int mid=(l+r)>>1;
            if (check(mid)) ans=mid,l=mid+1;else r=mid-1;
            //if (l<r) n2=0;
        }

        if (l==1)printf("?\n");else{
            for (int j=1;j<=n3;++j){
                int tmp1=q1[j];
                for (int i=tmp1;i<ans+tmp1;++i) putchar(a[i]+'a'-1);
                puts("");
            }
        }
        printf("\n");
    }
    return 0;
}

POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值