poj1080 Human Gene Functions

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
char str1[110];
char str2[110];
int num1;
int num2;
int d[110][110];
#define MAX -65530
void init()
{
    int i,j;
    for(i=0;i<=num1;i++)
    {
        for(j=0;j<=num2;j++)
        {
            d[i][j]=MAX;
        }
    }
}
int found(char ch1,char ch2)
{
    if(ch1==ch2)
    {
        return 5;
    }
    if((ch1=='A'&&ch2=='C')||(ch1=='C'&&ch2=='A'))
    {
        return -1;
    }
    if((ch1=='A'&&ch2=='G')||(ch1=='G'&&ch2=='A'))
    {
        return -2;
    }
    if((ch1=='A'&&ch2=='T')||(ch1=='T'&&ch2=='A'))
    {
        return -1;
    }
    if((ch1=='A'&&ch2=='-')||(ch1=='-'&&ch2=='A'))
    {
        return -3;
    }
    if((ch1=='C'&&ch2=='G')||(ch1=='G'&&ch2=='C'))
    {
        return -3;
    }
    if((ch1=='C'&&ch2=='T')||(ch1=='T'&&ch2=='C'))
    {
        return -2;
    }
    if((ch1=='C'&&ch2=='-')||(ch1=='-'&&ch2=='C'))
    {
        return -4;
    }
    if((ch1=='G'&&ch2=='T')||(ch1=='T'&&ch2=='G'))
    {
        return -2;
    }
    if((ch1=='G'&&ch2=='-')||(ch1=='-'&&ch2=='G'))
    {
        return -2;
    }
    if((ch1=='T'&&ch2=='-')||(ch1=='-'&&ch2=='T'))
    {
        return -1;
    }
}
void dp(int l1,int l2)
{
    int temp;
    if(d[l1][l2-1]==MAX)
    {
        dp(l1,l2-1);
    }
    temp=found(str2[l2-1],'-');
    if(d[l1][l2]<d[l1][l2-1]+temp)
    {
        d[l1][l2]=d[l1][l2-1]+temp;
    }
    if(d[l1-1][l2]==MAX)
    {
        dp(l1-1,l2);
    }
    temp=found(str1[l1-1],'-');
    if(d[l1][l2]<d[l1-1][l2]+temp)
    {
        d[l1][l2]=d[l1-1][l2]+temp;
    }
    if(d[l1-1][l2-1]==MAX)
    {
        dp(l1-1,l2-1);
    }
    temp=found(str1[l1-1],str2[l2-1]);
    if(d[l1][l2]<d[l1-1][l2-1]+temp)
    {
        d[l1][l2]=d[l1-1][l2-1]+temp;
    }
}
int main()
{
    int total;
    scanf("%d",&total);
    getchar();
    while(total--)
    {
        scanf("%d %s",&num1,str1);
        scanf("%d %s",&num2,str2);
        init();
        int i,j;
        d[0][0]=0;
        for(i=1;i<=num1;i++)
        {
            d[i][0]=found(str1[i-1],'-')+d[i-1][0];
        }
        for(i=1;i<=num2;i++)
        {
            d[0][i]=found(str2[i-1],'-')+d[0][i-1];
        }
        dp(num1,num2);
        printf("%d\n",d[num1][num2]);
    }
    return 0;
}
posted @ 2012-08-06 19:04  willzhang  阅读(122)  评论(0编辑  收藏  举报