Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <map>
  2. #include <string>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include "ArgumentManager.h"
  8. using namespace std;
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.    
  13.     ArgumentManager am(argc, argv);
  14.  
  15.     const string input = am.get("input");
  16.     const string output = am.get("output");
  17.  
  18.     ifstream inputfile;
  19.     inputfile.open(input);
  20.     ofstream outputfile;
  21.     outputfile.open(output);
  22.    
  23.    
  24.     string* lines = new string[4];
  25.     string line;
  26.     int i = 0;
  27.     int count = 0;
  28.    
  29.     while (!inputfile.eof())
  30.     {
  31.         getline(inputfile, line);
  32.         if (line.empty())
  33.             continue;
  34.         lines[i] = line;
  35.         i++;
  36.         count++;
  37.     }
  38.  
  39.    
  40.     int stSize1 = lines[0].length();
  41.     int stSize2 = lines[1].length();
  42.     int stSize3 = lines[2].length();
  43.     int stSize4 = lines[3].length();
  44.     int lcsLengthsize = 0;
  45.  
  46.     string input1 = lines[0];
  47.     string input2 = lines[1];
  48.     string input3 = lines[2];
  49.     string input4 = lines[3];
  50.  
  51.  
  52.     for (int z = 0 ; z < i; z++)
  53.         cout << lines[z] << endl;
  54.    
  55.    
  56.     if (count == 4) { // if 4 strings
  57.         int**** lcsMatrix = new int***[stSize1 + 1];
  58.         for (int a = 0; a <= stSize1; a++) {
  59.             lcsMatrix[a] = new int**[stSize2 + 1];
  60.             for (int b = 0; b <= stSize2; b++) {
  61.                 lcsMatrix[a][b] = new int*[stSize3 + 1];
  62.                 for (int c = 0; c <= stSize3; c++) {
  63.                     lcsMatrix[a][b][c] = new int[stSize4 + 1];
  64.                 }
  65.             }
  66.         }
  67.         for (int w = 0; w <= stSize1; w++) {
  68.             for (int x = 0; x <= stSize2; x++) {
  69.                 for (int y = 0; y <= stSize3; y++) {
  70.                     for (int z = 0; z <= stSize4; z++) {
  71.                         if (w == 0 || x == 0 || y == 0 || z == 0)
  72.                             lcsMatrix[w][x][y][z] = 0;
  73.  
  74.                         else if (input1.at(w - 1) == input2.at(x - 1) && input1.at(w - 1) == input3.at(y - 1) && input1.at(w - 1) == input4.at(z - 1))
  75.                             lcsMatrix[w][x][y][z] = lcsMatrix[w - 1][x - 1][y - 1][z - 1] + 1;
  76.  
  77.                         else
  78.                             lcsMatrix[w][x][y][z] = max(max(lcsMatrix[w - 1][x][y][z], lcsMatrix[w][x - 1][y][z]), max(lcsMatrix[w][x][y - 1][z], lcsMatrix[w][x][y][z - 1]));
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         lcsLengthsize = lcsMatrix[stSize1][stSize2][stSize3][stSize4];
  84.        
  85.  
  86.     }
  87.  
  88.  
  89.  
  90.     //This is for a 3d array
  91.  
  92.     else if (count == 3) {
  93.         int*** lcsMatrix = new int**[stSize1 + 1];
  94.         for (int a = 0; a <= stSize1; a++) {
  95.             lcsMatrix[a] = new int*[stSize2 + 1];
  96.             for (int b = 0; b <= stSize2; b++) {
  97.                 lcsMatrix[a][b] = new int[stSize3 + 1];
  98.             }
  99.         }
  100.         for (int x = 0; x <= stSize1; x++) {
  101.             for (int y = 0; y <= stSize2; y++) {
  102.                 for (int z = 0; z <= stSize3; z++) {
  103.                     if (x == 0 || y == 0 || z == 0)
  104.                         lcsMatrix[x][y][z] = 0;
  105.  
  106.                     else if (input1.at(x - 1) == input2.at(y - 1) && input1.at(x - 1) == input3.at(z - 1)) {
  107.                         lcsMatrix[x][y][z] = lcsMatrix[x - 1][y - 1][z - 1] + 1;
  108.                     }
  109.                     else
  110.                         lcsMatrix[x][y][z] = max(max(lcsMatrix[x - 1][y][z], lcsMatrix[x][y - 1][z]), lcsMatrix[x][y][z - 1]);
  111.                 }
  112.             }
  113.         }
  114.         lcsLengthsize = lcsMatrix[stSize1][stSize2][stSize3];
  115.        
  116.     }
  117.    
  118.     else if (count == 2) {//2d array
  119.         int** lcsMatrix = new int*[stSize1 + 1];
  120.         for (int a = 0; a <= stSize1; a++) {
  121.             lcsMatrix[a] = new int[stSize2 + 1];
  122.         }
  123.         for (int y = 0; y <= stSize1; y++) {
  124.             for (int z = 0; z <= stSize2; z++) {
  125.                 if (y == 0 || z == 0)
  126.                     lcsMatrix[y][z] = 0;
  127.                 else if (input1.at(y - 1) == input2.at(z - 1))
  128.                     lcsMatrix[y][z] = lcsMatrix[y - 1][z - 1] + 1;
  129.                 else
  130.                     lcsMatrix[y][z] = (max(lcsMatrix[y - 1][z], lcsMatrix[y][z - 1]));
  131.             }
  132.         }
  133.         lcsLengthsize = lcsMatrix[stSize1][stSize2];
  134.         //cout << lcsLengthsize << " 2d" << endl;
  135.     }
  136.  
  137.     ofstream out(output);
  138.     out << "Len: " << lcsLengthsize;
  139.    
  140.     system("pause");
  141.    
  142.     inputfile.close();
  143.     outputfile.close();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement