Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <string>
- #include <cstring>
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include "ArgumentManager.h"
- using namespace std;
- int main(int argc, char *argv[])
- {
- ArgumentManager am(argc, argv);
- const string input = am.get("input");
- const string output = am.get("output");
- ifstream inputfile;
- inputfile.open(input);
- ofstream outputfile;
- outputfile.open(output);
- string* lines = new string[4];
- string line;
- int i = 0;
- int count = 0;
- while (!inputfile.eof())
- {
- getline(inputfile, line);
- if (line.empty())
- continue;
- lines[i] = line;
- i++;
- count++;
- }
- int stSize1 = lines[0].length();
- int stSize2 = lines[1].length();
- int stSize3 = lines[2].length();
- int stSize4 = lines[3].length();
- int lcsLengthsize = 0;
- string input1 = lines[0];
- string input2 = lines[1];
- string input3 = lines[2];
- string input4 = lines[3];
- for (int z = 0 ; z < i; z++)
- cout << lines[z] << endl;
- if (count == 4) { // if 4 strings
- int**** lcsMatrix = new int***[stSize1 + 1];
- for (int a = 0; a <= stSize1; a++) {
- lcsMatrix[a] = new int**[stSize2 + 1];
- for (int b = 0; b <= stSize2; b++) {
- lcsMatrix[a][b] = new int*[stSize3 + 1];
- for (int c = 0; c <= stSize3; c++) {
- lcsMatrix[a][b][c] = new int[stSize4 + 1];
- }
- }
- }
- for (int w = 0; w <= stSize1; w++) {
- for (int x = 0; x <= stSize2; x++) {
- for (int y = 0; y <= stSize3; y++) {
- for (int z = 0; z <= stSize4; z++) {
- if (w == 0 || x == 0 || y == 0 || z == 0)
- lcsMatrix[w][x][y][z] = 0;
- 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))
- lcsMatrix[w][x][y][z] = lcsMatrix[w - 1][x - 1][y - 1][z - 1] + 1;
- else
- 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]));
- }
- }
- }
- }
- lcsLengthsize = lcsMatrix[stSize1][stSize2][stSize3][stSize4];
- }
- //This is for a 3d array
- else if (count == 3) {
- int*** lcsMatrix = new int**[stSize1 + 1];
- for (int a = 0; a <= stSize1; a++) {
- lcsMatrix[a] = new int*[stSize2 + 1];
- for (int b = 0; b <= stSize2; b++) {
- lcsMatrix[a][b] = new int[stSize3 + 1];
- }
- }
- for (int x = 0; x <= stSize1; x++) {
- for (int y = 0; y <= stSize2; y++) {
- for (int z = 0; z <= stSize3; z++) {
- if (x == 0 || y == 0 || z == 0)
- lcsMatrix[x][y][z] = 0;
- else if (input1.at(x - 1) == input2.at(y - 1) && input1.at(x - 1) == input3.at(z - 1)) {
- lcsMatrix[x][y][z] = lcsMatrix[x - 1][y - 1][z - 1] + 1;
- }
- else
- lcsMatrix[x][y][z] = max(max(lcsMatrix[x - 1][y][z], lcsMatrix[x][y - 1][z]), lcsMatrix[x][y][z - 1]);
- }
- }
- }
- lcsLengthsize = lcsMatrix[stSize1][stSize2][stSize3];
- }
- else if (count == 2) {//2d array
- int** lcsMatrix = new int*[stSize1 + 1];
- for (int a = 0; a <= stSize1; a++) {
- lcsMatrix[a] = new int[stSize2 + 1];
- }
- for (int y = 0; y <= stSize1; y++) {
- for (int z = 0; z <= stSize2; z++) {
- if (y == 0 || z == 0)
- lcsMatrix[y][z] = 0;
- else if (input1.at(y - 1) == input2.at(z - 1))
- lcsMatrix[y][z] = lcsMatrix[y - 1][z - 1] + 1;
- else
- lcsMatrix[y][z] = (max(lcsMatrix[y - 1][z], lcsMatrix[y][z - 1]));
- }
- }
- lcsLengthsize = lcsMatrix[stSize1][stSize2];
- //cout << lcsLengthsize << " 2d" << endl;
- }
- ofstream out(output);
- out << "Len: " << lcsLengthsize;
- system("pause");
- inputfile.close();
- outputfile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement