Skip to content

Instantly share code, notes, and snippets.

@truncs
Created March 21, 2012 01:12
Show Gist options
  • Save truncs/2143339 to your computer and use it in GitHub Desktop.
Save truncs/2143339 to your computer and use it in GitHub Desktop.
Parallel Prog
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
int n;
string string_n;
string s1;
string s2;
getline(cin, string_n);
n = atoi(string_n.c_str());
int N = n + 1;
int D[N][N], I[N][N], G[N][N];
const int g_e = 1;
const int g_i = 2;
cout<<getline(cin,s1);
cout<<getline(cin,s2);
cout<<n<<endl;
cout<<s1<<endl;
cout<<s2<<"here"<<endl;
for (int x = 0; x < 16; x++) {
cout<<s1[x];
}
cout<<endl;
for (int x = 0; x < 16; x++) {
cout<<s2[x];
}
cout<<endl;
// Forward Pass
for (int i = 0; i < N; i++)
{
for (int j=0; j< N; j++)
{
if (i == 0 && j == 0)
G[i][j] = 0;
if (i == 0 && j > 0) {
G[i][j] = g_i + g_e *j;
D[0][j] = G[0][j] + g_e;
}
if ( i > 0 && j == 0) {
G[i][j] = g_i + g_e *i;
I[i][0] = G[i][0] + g_e;
}
if (i > 0 && j > 0) {
D[i][j] = min(D[i - 1][j] , G[i - 1][j] + g_i) + g_e;
I[i][j] = min(I[i][j -1] , G[i][j - 1] + g_i) + g_e;
int s_cost = s1[i - 1] == s2[j -1] ? 0 : 1;
int temp = min(D[i][j], I[i][j]);
G[i][j] = min(G[i -1][j -1] + s_cost, temp);
}
}
}
// Backward pass
int i = N -1;
int j = N - 1;
// If the previous element is the diagnol element
// then print both the characters in the string,
// else if the previous element is the one in the
// insert element then insert insert a gap
// in the first string else it's the delete one
// then insert a gap in the second string
for (int i = 0; i < N; ++i){
for (int j=0; j< N; ++j) {
cout<<G[i][j]<<"\t";
}
cout<<endl;
}
cout<<"I Matrix"<<endl;
for (int i = 0; i < N; ++i){
for (int j=0; j< N; ++j) {
cout<<I[i][j]<<"\t";
}
cout<<endl;
}
cout<<"D Matrix"<<endl;
for (int i = 0; i < N; ++i){
for (int j=0; j< N; ++j) {
cout<<D[i][j]<<"\t";
}
cout<<endl;
}
while(i != 0 && j != 0) {
if (i == 0 && j > 0){
j = j -1;
cout<<"-"<<" "<<s2[j];
cout<<" "<<i<<"\t"<<j<<endl;
}
else if ( i > 0 && j == 0) {
i = i -1;
cout<<s1[i]<<" "<<"-";
cout<<" "<<i<<"\t"<<j<<endl;
}
else if (i > 0 && j > 0) {
int values[3] = {G[i -1][j - 1], G[i -1][j], G[i][j -1]};
int index = 0;
if (values[1] <= values[index])
index = 1;
if(values[2] < values[index])
index = 2;
switch(index) {
case 0:
i = i - 1;
j = j -1;
cout<<s1[i]<<" "<<s2[j];
cout<<" "<<i<<"\t"<<j<<endl;
break;
case 1:
i = i -1;
cout<<s1[i]<<" "<<"-";
cout<<" "<<i<<"\t"<<j<<endl;
break;
case 2:
j = j -1;
cout<<"-"<<" "<<s2[j];
cout<<" "<<i<<"\t"<<j<<endl;
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment