SOJ 1035 DNA matching

题目传送门在

给定一组DNA的单链序列,要找出总共能组成的双链数,既然是组成,说明用完一次(已经和互补链组合了)就不能再用,可以考虑用哈希的方法,哈希表是一个bool类型数组,默认为false,每读入一个新串,先到哈希表里面检验之前存不存在互补链,存在则将true值设为false表示已组合,否则将对应位置设为true

需要注意的一点是哈希函数的选择,一开始我只使用了BKDR,一直WA,想了想用了两个哈希表,用了另外一个哈希函数,两个不同哈希函数把不同值都映射到同一个位置大大减少,因此通过(看来题目数据量还是蛮大的)

#include <cstdio>
#include <cstring>
unsigned int BKDRHash(char *str) 
{ 
    unsigned int seed = 131;
    unsigned int hash = 0;
    while (*str) {
        hash = hash * seed + (*str++);
    }
    return (hash % 249997);
}
unsigned int RSHash(char *str) { 
    unsigned int b = 378551;
    unsigned int a = 63689;
    unsigned int hash = 0;
    while (*str) { 
        hash = hash * a + (*str++); 
        a *= b;
    }
    return (hash % 249997); 
}

void transform(char *str) {
    for (int i = 0; i < strlen(str); ++i) {
        switch (str[i]) {
            case 'A':
                str[i] = 'T';
                break;
            case 'T':
                str[i] = 'A';
                break;
            case 'C':
                str[i] = 'G';
                break;
            case 'G':
                str[i] = 'C';
                break;
        }
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        bool have1[249997], have2[249997];
        for (int i = 0; i < n; ++i) {
            have1[i] = have2[i] = false;
        }
        int total = 0;
        char chains[101][101];
        for (int i = 0; i < n; ++i) {
            scanf("%s", chains[i]);
            int post1 = BKDRHash(chains[i]);
            int post2 = RSHash(chains[i]);
            if (have1[post1] && have2[post2]) {
                total++;
                have1[post1] = have2[post2] = false;
            } else {
                transform(chains[i]);
                post1 = BKDRHash(chains[i]);
                post2 = RSHash(chains[i]);
                have1[post1] = true;
                have2[post2] = true;
            }
        }
        printf("%d\n", total);
    }
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值