SOJ 1035 DNA matching

来源:互联网 发布:手机空调遥控器软件 编辑:程序博客网 时间:2024/05/14 18:35

题目传送门在此

给定一组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
原创粉丝点击