-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15.GAP.cpp
109 lines (86 loc) · 2.24 KB
/
15.GAP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* This program finds the best global alingment for two strings
* using Dynamic Programming
*
* Coded by: Abdurrezak EFE
* */
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
using namespace std;
int d[10000][10000]; // dynamic programming table
char t[10000][10000]; //traceback table
int mismatch_pen = 3;
int match_rew = 5;
int indel_pen = 1;
string str1;
string str2;
string reverser(string str)
{
string s="";
for(int i = str.size()-1;i>-1;i--)
s+=str[i];
return s;
}
void GA(string s1, string s2)
{
for(int i=0;i<=s1.size();i++) // initiating first row as zeros
d[0][i] = (-1)*i*indel_pen, t[0][i] = 'l';
for(int i=0;i<=s2.size();i++) // initiating first column as zero
d[i][0] = (-1)*i*indel_pen, t[i][0] = 'u';
d[0][0] = 0;
for(int i=1; i<=s1.size(); i++)
{
for(int j=1; j<=s2.size(); j++)
{
if(s1[i-1] == s2[j-1])
d[i][j] = max(max(d[i-1][j-1] + match_rew, d[i-1][j]-indel_pen), d[i][j-1]-indel_pen);
else
d[i][j] = max(max(d[i-1][j]-indel_pen, d[i][j-1]-indel_pen), d[i-1][j-1]-mismatch_pen);
if(d[i][j] == d[i-1][j-1]-mismatch_pen ) //mismatch
t[i][j] = 'n';
else if(d[i][j] == d[i-1][j-1] + match_rew && s1[i-1] == s2[j-1]) //match
t[i][j] = 'p';
else if(d[i][j] == d[i-1][j] - indel_pen) //indel
t[i][j] = 'u';
else
t[i][j] = 'l';
//cout << d[i][j] << " ";
}
//cout << endl;
}
str1=""; //getting alignment of s1
str2=""; //getting alignment of s2
int i= s1.size();
int j= s2.size();
while(i>0 || j>0)
{
if(t[i][j] == 'u')
{
str1 += s1[i-1];
str2 += '-';
i--;
}
else if(t[i][j] == 'l')
{
str1 += '-';
str2 += s2[j-1];
j--;
}else
{
str1 += s1[i-1];
str2 += s2[j-1];
i--,j--;
}
}
str1 = reverser(str1);
str2 = reverser(str2);
}
int main()
{
string s1,s2;
cin >> s1 >> s2;
GA(s1,s2);
cout << str1 << endl << str2 << endl;
}