病毒DNA检测

[基本要求]

  1. 从文件中读取患者DNA作为主串,读取病毒DNA序列作为模式串,均以字符串形式存储;
  2. 依次对“病毒感染检测输入数据.txt”中的11组患者DNA和病毒DNA进行模式匹配Index操作;
  3. 模式匹配过程可以采用BF算法,也可以根据模式串内容构造Next数组使用KMP算法实现;
  4. 完成对每组测试数据的模式匹配后,根据匹配结果输出成功“YES”或失败“NO”标识;

能够对每组数据匹配结果进行分析,解释匹配成功或失败程序的执行过程。

记得创建一个记事本名为病毒感染检测输入数据内容如下:

10

baa bbaabbba 
baa aaabbbba
aabb abceaabb
aabb abaabcea
abcd cdabbbab
abcd cabbbbab
abcde bcdedbda
acc bdedbcda
cde cdcdcdec
cced cdccdcce

#include<iostream>
#include<fstream>
#include<string.h>
#include<utility>

using namespace std;

typedef struct{
    char ch[600];		//若是非空串,则按串长分配存储区,否则ch为NULL
    int len;			//串长度
} HString;

int* GetNextVal(const char *s, int len){
    int *next = new int[len];
    int i = 1;
    int j = 0;
    next[1] = 0;
    while(i<len){
        if(j==0 || s[i]==s[j]){
            ++i;
            ++j;
            next[i] = j;
        }else{
            j = next[j];
        }
    }
    return next;
}

int KMP(const char *s, int slen,const char *t,int tlen){
    int i,j;
    int *next = GetNextVal(t, tlen);
   
    i = 1;
    j = 1;
    while(i<=slen && j<=tlen){
        if(j==0 || s[i]==t[j]){
            ++i;
            ++j;
        }else{
            j = next[j];
        }
    }
    if(j>tlen)
        return i-tlen;
    return 0;
}

void Virus_detection(){ 
   int num,m,flag,i,j; char Vir[600];
   HString Virus,Person,temp;
   ifstream inFile("病毒感染检测输入数据.txt");
   ofstream outFile("病毒感染检测输出结果.txt");
   inFile>>num;//读取待检测的任务数
    //依次检测每对病毒DNA和人的DNA是否匹配
   while(num--) {
       inFile>>Virus.ch+1;//读取病毒DNA序列
       inFile>>Person.ch+1;//读取人的DNA序列
       strcpy(Vir,Virus.ch);
       Virus.len=strlen(Virus.ch)-1;
       Person.len=strlen(Person.ch)-1;
       flag=0;//用来标识是否匹配,初始为0,匹配后为非0
       m=Virus.len;
       for(i=m+1,j=1;j<=m;j++) Virus.ch[i++]=Virus.ch[j];
      //因病毒为环状,故将病毒的长度扩大2倍
      //即可线性取到所有长度为Virus.len的字符串
      Virus.ch[2*m+1]='\0'; //添加结束符号
      for(i=0;i<m;i++){
          for (j = 1; j <= m; j++) {
              temp.ch[j] = Virus.ch[i + j];
          }
         //取长为Virus.len的环形字符串
         //即Virus.ch[0]-- Virus.ch[Virus.len-1],
         //Virus.ch[1]-- Virus.ch[0], Virus.ch[2]-- Virus.ch[1]...
         temp.ch[m+1]='\0'; //添加结束符号
         temp.len=strlen(temp.ch)-1;
        flag=KMP(Person.ch,Person.len,temp.ch,temp.len);  //模式匹配
         if(flag) break; //匹配即可退出循环
      }
      if (flag) {
        outFile<<Vir+1<<" "<<Person.ch+1<<" "<<"YES"<<endl;
      }else {
        outFile<<Vir+1<<" "<<Person.ch+1<<" "<<"NO"<<endl;
      }   
   }
}


int main()
{
    Virus_detection();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值