-
Notifications
You must be signed in to change notification settings - Fork 0
/
superstr.c
117 lines (104 loc) · 1.93 KB
/
superstr.c
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
#include <stdio.h>
#include <stdlib.h>
#define size 1024
char** base;
int length(int i)
{
int length = 0;
while (*(*(base + i) + length) != 0)
length++;
return length;
}
int wordcmp(const char *Aword, const char *Bword){
while (*Aword && *Bword && *Aword == *Bword){
Aword++;
Bword++;
}
return *Aword - *Bword;
}
char* subword(int word, int left, int right)
{
char* subword = (char*)malloc(size * sizeof(char));
int i = 0;
int j = left;
while (j < right + 1) {
*(subword + i) = *(*(base + word) + j);
i++;
j++;
}
*(subword + i) = 0;
return subword;
}
int overlap(int a, int b)
{
int Aword = length(a) - 1;
int Bword = length(b) - 1;
int overlap = 0;
int i = Aword;
int j = 0;
char* suff,* preff;
while ((i > 0) && (j < Bword + 1)) {
suff = subword(a, i, Aword);
preff = subword(b, 0, j);
//printf("%s %s\n", suff, preff);
if (wordcmp(suff, preff) == 0)
overlap = j + 1;
free(suff);
free(preff);
i--;
j++;
}
return overlap;
}
void merge(int a, int b, int j) {
int i = 0;
while(*(*(base + a) + i) != 0)
i++;
while(*(*(base + b) + j) != 0){
*(*(base + a) + i) = *(*(base + b) + j);
i++;
j++;
}
*(*(base + a) + i) = 0;
**(base + b) = 0;
}
void superstr(int nel)
{
int half = 0;
while (half < (nel - 1)) {
int a = 0, b = 0;
int maxlap = -1;
for (int i = 0; i < nel; i++) {
for (int j = 0; j < nel; j++) {
if ((i != j) && (**(base + i) != 0) && (**(base + j) != 0)) {
int h = overlap(i, j);
if (h > maxlap) {
maxlap = h;
a = i;
b = j;
}
}
}
}
merge(a, b, maxlap);
half++;
}
}
int main()
{
int nel;
scanf("%d ", &nel);
base = (char**)malloc(nel * sizeof(char*));
for (int i = 0; i < nel; i++){
*(base + i) = (char*)malloc(size * sizeof(char));
scanf("%s\n", *(base + i));
}
superstr(nel);
for (int i = 0; i < nel; i++) {
if (**(base + i) != 0)
printf("%d\n", length(i));
free(*(base + i));
}
free(base);
return 0;
}