-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.hpp
More file actions
167 lines (135 loc) · 4.22 KB
/
lcs.hpp
File metadata and controls
167 lines (135 loc) · 4.22 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <string>
#include <deque>
#include <cstring>
#include <vector>
//System Header
#include <unistd.h>
using namespace std;
#ifndef BUFSIZE
#define BUFSIZE 1024
#endif
#ifndef PRINT_SIZE
#define PRINT_SIZE 10
#endif
#ifndef DEBUG
#define DEBUG false
#endif
bool lcsDriver(string a, string b);
bool printLcsFile(string a, string b);
/** Implementation **/
bool lcsDriver(string a, string b) {
if (access(a.c_str(), F_OK) < 0) {
fprintf (stderr, "%s is not existed\n",a.c_str());
return false;
}
if (access(b.c_str(), F_OK) < 0) {
fprintf (stderr, "%s is not existed\n",b.c_str());
return false;
}
return printLcsFile(a, b);
}
bool printLcsFile(string a, string b) {
char buf[BUFSIZE] = {0,};
int** lcsArray;
bool* sameCheckerA;
bool* sameCheckerB;
int szA, szB;
FILE *fp1, *fp2;
if ((fp1 = fopen(a.c_str(), "r")) == NULL) {
fprintf (stderr, "fopen error for %s\n", a.c_str());
return false;
}
if ((fp2 = fopen(b.c_str(), "r")) == NULL) {
fprintf (stderr, "fopen error for %s\n", b.c_str());
return false;
}
deque<string> lineA, lineB;
while(!feof(fp1)) {
if (fgets(buf, BUFSIZE, fp1) == NULL)
break;
lineA.push_back(buf);
memset(buf, 0, BUFSIZE);
}
while(!feof(fp2)) {
if (fgets(buf, BUFSIZE, fp2) == NULL)
break;
lineB.push_back(buf);
memset(buf, 0, BUFSIZE);
}
fclose(fp1);
fclose(fp2);
szA = (int)lineA.size(), szB = (int)lineB.size();
int maxCnt = 0;
sameCheckerA = (bool*)calloc(szA, sizeof(bool));
sameCheckerB = (bool*)calloc(szB, sizeof(bool));
lcsArray = (int**)calloc(szA+1, sizeof(int*));
//lcsArray 구성
for (int i = 0 ; i <= szA ; i++) {
lcsArray[i] = (int*)calloc(szB+1, sizeof(int));
if (!i) continue;
for (int j = 1 ; j <= szB ; j++) {
if (!lineA.at(i-1).compare(lineB.at(j-1)))
lcsArray[i][j] = lcsArray[i-1][j-1] + 1;
else
lcsArray[i][j] = max(lcsArray[i-1][j], lcsArray[i][j-1]);
}
}
int i=szA ,j=szB;
deque<string> lcsQ;
//왼쪽 우선이동 : 앞 파일이 우선이기 때문
while (i >= 1 && j >= 1) {
if (lcsArray[i][j] == lcsArray[i-1][j]) i--;
else if (lcsArray[i][j] == lcsArray[i][j-1]) j--;
else {
if (lcsArray[i][j] > lcsArray[i-1][j-1])
sameCheckerA[i-1] = sameCheckerB[j-1] = true;
i--,j--;
}
}
deque<int> notSamePos;
for (int j = 0 ; j < szB ; j++)
if (!sameCheckerB[j])
notSamePos.push_back(j);
int idxA=0, idxB=0;
while(idxA < szA || idxB < szB) {
for (int cnt = 0 ; idxA < szA && !sameCheckerA[idxA] ; cnt++, idxA++) {
if (!cnt) {
cout<<"======================="<<endl;
cout<<"<"<<a<<" file Differ Part>"<<endl;
}
cout<<"["<<idxA+1<<".] "<<lineA.at(idxA);
}
for (int cnt = 0 ; idxB < szB && !sameCheckerB[idxB] ; cnt++, idxB++) {
if (!cnt) {
cout<<"======================="<<endl;
cout<<"<"<<b<<" Differ Part>"<<endl;
}
cout<<"["<<idxB+1<<".] "<<lineB.at(idxB);
}
for (int cnt = 0 ;
(idxA < szA && sameCheckerA[idxA]) && (idxB < szB && sameCheckerB[idxB]) ;
idxA++, idxB++, cnt++) {
if (!cnt) {
cout<<"======================="<<endl;
//cout<<"{동일한 부분들 ...}"<<endl;
}
if (cnt < PRINT_SIZE) {
if (idxA < szA && sameCheckerA[idxA])
cout<<lineA.at(idxA);
else if (idxB < szB && sameCheckerB[idxB])
cout<<lineB.at(idxB);
}
if (cnt == PRINT_SIZE-1)
cout<<"..."<<endl;
}
}
cout<<endl<<" >>> Analysis finshed <<< "<<endl;
return true;
}
#if DEBUG
int main(void) {
printLcsFile("lcs.cpp", "/home/junhyeong/backup/ScriptReader/lcs.cpp_1240203130428");
exit(0);
}
#endif