forked from mojadita/twelvedays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathct.c
134 lines (119 loc) · 3.45 KB
/
ct.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* $Id: main.c.m4,v 1.7 2005/11/07 19:39:53 luis Exp $
* Author: Luis Colorado <lc@luiscoloradosistemas.com>
* Date: Fri Dec 12 14:51:41 EET 2014
*
* Disclaimer:
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define IN_CC_C
/* Standard include files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <avl.h>
/* constants */
#define MAX 65536
#define D(X) __FILE__":%d:" X, __LINE__
/* types */
struct node {
char *s;
int l;
int n;
};
/* prototypes */
struct node *new_node(char *s, int l);
/* variables */
char buffer[MAX];
/* functions */
struct node *new_node(char *s, int l)
{
struct node *res;
assert(res = malloc(sizeof(struct node)));
res->s = s;
res->l = l;
res->n = 1;
return res;
} /* new_node */
int node_fcomp(const void *n1, const void *n2)
{
const struct node *p1 = n1;
const struct node *p2 = n2;
int i;
for (i = 0; i < p1->l && i < p2->l; i++)
if (p1->s[i] != p2->s[i]) break;
/* if we have reached the end on both strings */
if (i == p1->l && i == p2->l) {
return 0;
}
/* if we have not reached the end on any string */
if (i < p1->l && i < p2->l) {
return p1->s[i] - p2->s[i];
}
/* we have reached the end on one and not in the other */
return p1->l < p2->l
? -1
: +1;
} /* node_fcomp */
int node_fprint(const void *p, FILE *f)
{
const struct node *n = p;
return fprintf(f, "%.*s", n->l, n->s);
} /* node_fprint */
/* main program */
int main (int argc, char **argv)
{
char *p;
AVL_TREE t;
AVL_ITERATOR it;
int n = read(0, buffer, sizeof buffer - 1);
if (n < 0) {
fprintf(stderr,
D("read:%s(errno=%d)\n"),
strerror(errno), errno);
exit(EXIT_FAILURE);
} /* if */
buffer[n] = 0;
t = new_avl_tree(node_fcomp, NULL, NULL, node_fprint);
for (p = buffer; *p; p++, n--) {
int l;
for (l = 1; l < n; l++) {
struct node nod;
nod.s = p;
nod.l = *p ? l : 0;
struct node *n = avl_tree_get(t, &nod);
if (!n) {
n = new_node(p, l);
avl_tree_put(t, n, n);
} /* if */
/* if it doesn't share space with the previous
* add it. */
if (n->s + n->l <= p)
n->n++;
}
} /* for */
n = 0;
for (it = avl_tree_first(t); it; it = avl_iterator_next(it)) {
struct node *N = avl_iterator_data(it);
int f = (N->l - 1)*(N->n - 1);
if (f >= n) {
printf("l=%04d n=%04d f=%4d: [%.*s]\n",
N->l, N->n, f, N->l, N->s);
n = f;
} /* if */
} /* for */
} /* main */
/* $Id: main.c.m4,v 1.7 2005/11/07 19:39:53 luis Exp $ */