Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e3b464d

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent fa4cfdd commit e3b464d

File tree

2 files changed

+75
-232
lines changed

2 files changed

+75
-232
lines changed

0139_word_break/word_break.c

Lines changed: 21 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -3,162 +3,46 @@
33
#include <stdbool.h>
44
#include <string.h>
55

6-
#define container_of(ptr, type, member) \
7-
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
86

9-
#define list_entry(ptr, type, member) \
10-
container_of(ptr, type, member)
11-
12-
#define list_for_each(p, head) \
13-
for (p = (head)->next; p != (head); p = p->next)
14-
15-
#define list_for_each_safe(p, n, head) \
16-
for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
17-
18-
struct list_head {
19-
struct list_head *next, *prev;
20-
};
21-
22-
static inline void INIT_LIST_HEAD(struct list_head *list)
23-
{
24-
list->next = list->prev = list;
25-
}
26-
27-
static inline int list_empty(const struct list_head *head)
28-
{
29-
return (head->next == head);
30-
}
31-
32-
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
33-
{
34-
next->prev = new;
35-
new->next = next;
36-
new->prev = prev;
37-
prev->next = new;
38-
}
39-
40-
static inline void list_add(struct list_head *_new, struct list_head *head)
41-
{
42-
__list_add(_new, head, head->next);
43-
}
44-
45-
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
46-
{
47-
__list_add(_new, head->prev, head);
48-
}
49-
50-
static inline void __list_del(struct list_head *entry)
51-
{
52-
entry->next->prev = entry->prev;
53-
entry->prev->next = entry->next;
54-
}
55-
56-
static inline void list_del(struct list_head *entry)
57-
{
58-
__list_del(entry);
59-
entry->next = entry->prev = NULL;
60-
}
61-
62-
struct word_node {
63-
char *word;
64-
struct list_head link;
65-
};
66-
67-
struct dfs_cache {
68-
int num;
69-
int cap;
70-
struct list_head **heads;
71-
};
72-
73-
static struct dfs_cache *resize(struct dfs_cache **caches, int index)
74-
{
75-
int i;
76-
struct dfs_cache *cache = caches[index];
77-
if (cache->num + 1 > cache->cap) {
78-
cache->cap *= 2;
79-
struct list_head **heads = malloc(cache->cap * sizeof(*heads));
80-
for (i = 0; i < cache->cap; i++) {
81-
if (i < cache->num) {
82-
heads[i] = cache->heads[i];
83-
} else {
84-
heads[i] = malloc(sizeof(struct list_head));
85-
INIT_LIST_HEAD(heads[i]);
86-
}
87-
}
88-
free(cache->heads);
89-
cache->heads = heads;
90-
}
91-
92-
return cache;
93-
}
94-
95-
static struct dfs_cache *dfs(char *s, char **words, int *sizes, int num,
96-
struct dfs_cache **caches, int index)
97-
{
7+
static int dfs(char *s, char **words, int *lens, int size, bool *ends, int index)
8+
{
989
int i, j;
99-
struct word_node *wn;
100-
struct dfs_cache *result;
101-
10210
if (*s == '\0') {
103-
return NULL;
104-
} else if (caches[index] != NULL) {
105-
return caches[index];
11+
return true;
12+
} else if (!ends[index]) {
13+
return false;
10614
} else {
107-
result = malloc(sizeof(*result));
108-
result->num = 0;
109-
result->cap = 1;
110-
result->heads = malloc(sizeof(struct list_head *));
111-
result->heads[0] = malloc(sizeof(struct list_head));
112-
INIT_LIST_HEAD(result->heads[0]);
113-
caches[index] = result;
114-
for (i = 0; i < num; i++) {
115-
if (!memcmp(s, words[i], sizes[i])) {
116-
struct dfs_cache *next = dfs(s + sizes[i], words, sizes, num, caches, index + sizes[i]);
117-
if (next != NULL) {
118-
int k = result->num;
119-
for (j = k; j < k + next->num; j++) {
120-
result = resize(caches, index);
121-
wn = malloc(sizeof(*wn));
122-
wn->word = words[i];
123-
list_add(&wn->link, result->heads[j]);
124-
125-
struct list_head *p;
126-
list_for_each(p, next->heads[j - k]) {
127-
struct word_node *wnn = list_entry(p, struct word_node, link);
128-
wn = malloc(sizeof(*wn));
129-
wn->word = wnn->word;
130-
list_add_tail(&wn->link, result->heads[j]);
131-
}
132-
result->num++;
133-
}
134-
} else {
135-
return NULL;
15+
for (i = 0; i < size; i++) {
16+
ends[index] = false;
17+
if (!strncmp(s, words[i], lens[i])) {
18+
/* post-order traverse */
19+
bool ok = dfs(s + lens[i], words, lens, size, ends, index + lens[i]);
20+
if (ok) {
21+
/* string s all matched */
22+
return true;
13623
}
13724
}
13825
}
13926

140-
return result;
27+
return ends[index];
14128
}
14229
}
14330

144-
static bool wordBreak(char* s, char** wordDict, int wordDictSize)
31+
bool wordBreak(char * s, char ** wordDict, int wordDictSize)
14532
{
14633
if (wordDictSize == 0) {
14734
return false;
14835
}
14936

150-
int i, total = 0;
151-
int len = strlen(s);
152-
int *sizes = malloc(wordDictSize * sizeof(int));
153-
37+
int i, len = strlen(s);
38+
int *lens = malloc(wordDictSize * sizeof(int));
15439
for (i = 0; i < wordDictSize; i++) {
155-
sizes[i] = strlen(wordDict[i]);
156-
total += sizes[i];
40+
lens[i] = strlen(wordDict[i]);
15741
}
15842

159-
struct dfs_cache **caches = malloc(len * sizeof(*caches));
160-
memset(caches, 0, len * sizeof(*caches));
161-
return dfs(s, wordDict, sizes, wordDictSize, caches, 0) == NULL;
43+
bool *ends = malloc(len);
44+
memset(ends, true, len);
45+
return dfs(s, wordDict, lens, wordDictSize, ends, 0);
16246
}
16347

16448
int main(int argc, char **argv)

0140_word_break_ii/word_break.c

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
56
#define container_of(ptr, type, member) \
67
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
78

@@ -11,13 +12,20 @@
1112
#define list_for_each(p, head) \
1213
for (p = (head)->next; p != (head); p = p->next)
1314

14-
#define list_for_each_safe(p, n, head) \
15-
for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
16-
1715
struct list_head {
1816
struct list_head *next, *prev;
1917
};
2018

19+
struct word_node {
20+
char *word;
21+
struct list_head link;
22+
};
23+
24+
struct solution {
25+
int count;
26+
struct list_head heads[];
27+
};
28+
2129
static inline void INIT_LIST_HEAD(struct list_head *list)
2230
{
2331
list->next = list->prev = list;
@@ -46,107 +54,58 @@ static inline void list_add_tail(struct list_head *_new, struct list_head *head)
4654
__list_add(_new, head->prev, head);
4755
}
4856

49-
static inline void __list_del(struct list_head *entry)
57+
static void new_word_add(struct list_head *head, char *word)
5058
{
51-
entry->next->prev = entry->prev;
52-
entry->prev->next = entry->next;
59+
struct word_node *wn = malloc(sizeof(*wn));
60+
wn->word = word;
61+
list_add_tail(&wn->link, head);
5362
}
5463

55-
static inline void list_del(struct list_head *entry)
56-
{
57-
__list_del(entry);
58-
entry->next = entry->prev = NULL;
59-
}
60-
61-
struct word_node {
62-
char *word;
63-
struct list_head link;
64-
};
65-
66-
struct dfs_cache {
67-
int num;
68-
int cap;
69-
struct list_head **heads;
70-
};
71-
72-
static struct dfs_cache *resize(struct dfs_cache **caches, int index)
73-
{
74-
int i;
75-
struct dfs_cache *cache = caches[index];
76-
if (cache->num + 1 > cache->cap) {
77-
cache->cap *= 2;
78-
struct list_head **heads = malloc(cache->cap * sizeof(*heads));
79-
for (i = 0; i < cache->cap; i++) {
80-
if (i < cache->num) {
81-
heads[i] = cache->heads[i];
82-
} else {
83-
heads[i] = malloc(sizeof(struct list_head));
84-
INIT_LIST_HEAD(heads[i]);
85-
}
86-
}
87-
free(cache->heads);
88-
cache->heads = heads;
89-
}
90-
91-
return cache;
92-
}
93-
94-
static struct dfs_cache *dfs(char *s, char **words, int *sizes, int num,
95-
struct dfs_cache **caches, int index)
64+
static struct solution *dfs(char *s, char **words, int *lens, int size,
65+
struct solution **sols, int index)
9666
{
9767
int i, j;
98-
struct word_node *wn;
99-
struct dfs_cache *result;
100-
10168
if (*s == '\0') {
10269
return NULL;
103-
} else if (caches[index] != NULL) {
104-
return caches[index];
70+
} else if (sols[index] != NULL) {
71+
return sols[index];
10572
} else {
106-
result = malloc(sizeof(*result));
107-
result->num = 0;
108-
result->cap = 1;
109-
result->heads = malloc(sizeof(struct list_head *));
110-
result->heads[0] = malloc(sizeof(struct list_head));
111-
INIT_LIST_HEAD(result->heads[0]);
112-
caches[index] = result;
113-
for (i = 0; i < num; i++) {
114-
if (!memcmp(s, words[i], sizes[i])) {
115-
struct dfs_cache *next = dfs(s + sizes[i], words, sizes, num, caches, index + sizes[i]);
116-
if (next != NULL) {
117-
int k = result->num;
118-
for (j = k; j < k + next->num; j++) {
119-
result = resize(caches, index);
120-
wn = malloc(sizeof(*wn));
121-
wn->word = words[i];
122-
list_add(&wn->link, result->heads[j]);
123-
73+
struct solution *sol = malloc(sizeof(*sol) + 60 * sizeof(struct list_head));
74+
sol->count = 0;
75+
sols[index] = sol;
76+
for (i = 0; i < size; i++) {
77+
if (!strncmp(s, words[i], lens[i])) {
78+
/* post-order traverse */
79+
struct solution *sub_sol = dfs(s + lens[i], words, lens, size, sols, index + lens[i]);
80+
if (sub_sol != NULL) {
81+
int k = sol->count;
82+
for (j = k; j < k + sub_sol->count; j++) {
83+
/* Append all sub-solutions */
84+
INIT_LIST_HEAD(&sol->heads[j]);
85+
new_word_add(&sol->heads[j], words[i]);
12486
struct list_head *p;
125-
list_for_each(p, next->heads[j - k]) {
126-
struct word_node *wnn = list_entry(p, struct word_node, link);
127-
wn = malloc(sizeof(*wn));
128-
wn->word = wnn->word;
129-
list_add_tail(&wn->link, result->heads[j]);
87+
list_for_each(p, &sub_sol->heads[j - k]) {
88+
struct word_node *wn = list_entry(p, struct word_node, link);
89+
new_word_add(&sol->heads[j], wn->word);
13090
}
131-
result->num++;
91+
sol->count++;
13292
}
13393
} else {
134-
wn = malloc(sizeof(*wn));
135-
wn->word = words[i];
136-
list_add(&wn->link, result->heads[result->num++]);
94+
/* leaf node */
95+
INIT_LIST_HEAD(&sol->heads[0]);
96+
new_word_add(&sol->heads[sol->count++], words[i]);
13797
}
13898
}
13999
}
140-
141-
return result;
100+
return sol;
142101
}
143102
}
144103

145104
/**
146-
** Return an array of size *returnSize.
147-
** Note: The returned array must be malloced, assume caller calls free().
148-
**/
149-
static char **wordBreak(char* s, char** wordDict, int wordDictSize, int *returnSize)
105+
* Return an array of size *returnSize.
106+
* Note: The returned array must be malloced, assume caller calls free().
107+
*/
108+
char **wordBreak(char* s, char** wordDict, int wordDictSize, int *returnSize)
150109
{
151110
if (wordDictSize == 0) {
152111
*returnSize = 0;
@@ -155,24 +114,24 @@ static char **wordBreak(char* s, char** wordDict, int wordDictSize, int *returnS
155114

156115
int i, total = 0;
157116
int len = strlen(s);
158-
int *sizes = malloc(wordDictSize * sizeof(int));
117+
int *lens = malloc(wordDictSize * sizeof(int));
159118

160119
/* Add into hash list */
161120
for (i = 0; i < wordDictSize; i++) {
162-
sizes[i] = strlen(wordDict[i]);
163-
total += sizes[i];
121+
lens[i] = strlen(wordDict[i]);
122+
total += lens[i];
164123
}
165124

166-
struct dfs_cache **caches = malloc(len * sizeof(*caches));
167-
memset(caches, 0, len * sizeof(*caches));
168-
struct dfs_cache *cache = dfs(s, wordDict, sizes, wordDictSize, caches, 0);
125+
struct solution **sols = malloc(len * sizeof(void *));
126+
memset(sols, 0, len * sizeof(void *));
127+
struct solution *sol = dfs(s, wordDict, lens, wordDictSize, sols, 0);
169128

170-
char **results = malloc(cache->num * sizeof(char *));
171-
for (i = 0; i < cache->num; i++) {
129+
char **results = malloc(sol->count * sizeof(char *));
130+
for (i = 0; i < sol->count; i++) {
172131
results[i] = malloc(total + 100);
173132
char *p = results[i];
174133
struct list_head *n;
175-
list_for_each(n, cache->heads[i]) {
134+
list_for_each(n, &sol->heads[i]) {
176135
struct word_node *wn = list_entry(n, struct word_node, link);
177136
char *q = wn->word;
178137
while ((*p++ = *q++) != '\0') {}
@@ -181,7 +140,7 @@ static char **wordBreak(char* s, char** wordDict, int wordDictSize, int *returnS
181140
*(p - 1) = '\0';
182141
}
183142

184-
*returnSize = cache->num;
143+
*returnSize = sol->count;
185144
return results;
186145
}
187146

0 commit comments

Comments
 (0)