-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
139 lines (122 loc) · 3.08 KB
/
Sequence.cpp
File metadata and controls
139 lines (122 loc) · 3.08 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "Sequence.h"
Sequence::Sequence()
{
}
Sequence::Sequence(int length){
cout << "Enter the sequence" << endl;
seq = new char[length];
char p;
for(int i=0; i<length; i++){
cin >> p;
seq[i] = p;
}
}
Sequence::Sequence(Sequence& rhs){
int length = 0;
while(rhs.seq[length] != '\0'){
length++;
}
length++;
for(int i=0; i<length; i++){
seq[i] = rhs.seq[i];
}
}
Sequence::~Sequence()
{
delete[] seq;
}
char* Align(Sequence * s1, Sequence * s2, int n, int m){
int **arr = new int *[m+1];
for(int i=0; i<n+1; i++){
arr[i] = new int[m+1];
}
for(int i=0; i<m+1; i++){
for(int j=0; j<n+1; j++){
arr[i][j] = 0;
}
}
for(int i=1; i<m+1; i++){
for(int j=1; j<n+1; j++){
if(s2->seq[i-1] == s1->seq[j-1]){
arr[i][j] = arr[i-1][j-1] +1;
}else{
if(arr[i-1][j] > arr[i][j-1]){
arr[i][j] = arr[i-1][j];
}else{
arr[i][j] = arr[i][j-1];
}
}
}
}
int lengthOfSeq = arr[m][n];
int loop = lengthOfSeq;
char *validSeq = new char[lengthOfSeq];
validSeq[lengthOfSeq] = '\0'; ///come back
while(lengthOfSeq >= 0 && m != 0 && n != 0){
if(arr[m][n] == arr[m-1][n] && arr[m][n] == arr[m][n-1]){
n--;
}else if(arr[m][n-1] == arr[m-1][n] && arr[m][n] > arr[m][n-1]){
validSeq[lengthOfSeq-1] = s2->seq[m-1];
m--;
n--;
lengthOfSeq--;
}else{
if(arr[m][n-1] > arr[m-1][n]){
n--;
}else{
m--;
}
}
}
for(int i=0; i<loop; i++){
cout << validSeq[i] << ' ';
}
cout << endl;
return validSeq;
/**int L[m+1][n+1];
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0){
L[i][j] = 0;
}
else if (s1->seq[i-1] == s2->seq[j-1]){
L[i][j] = L[i-1][j-1] + 1;
}
else{
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
}
int size1 = L[m][n];
char lcs[size1+1];
lcs[size1] = '\0'; // Set the terminating character
int loop = size1;
// Start from the right-most-bottom-most corner and
// one by one store characters in lcs[]
int i = m, j = n;
while (i > 0 && j > 0)
{
if (s1->seq[i-1] == s2->seq[j-1])
{
lcs[i-1] = s1->seq[i-1]; // Put current character in result
i--; j--; size1--;
}
// If not same, then find the larger of two and
// go in the direction of larger value
else if (L[i-1][j] > L[i][j-1]){
i--;
}
else{
j--;
}
}
for(int i=0; i<loop+1; i++){
cout << lcs[i] << ' ';
}
cout << endl;
return lcs;
// Print the lcs
///cout << "LCS of " << s1 << " and " << s2 << " is " << lcs;*/
}