-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindormic2.c
258 lines (235 loc) · 5.03 KB
/
palindormic2.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_MAP_SIZE 10007
typedef struct HashMapNode
{
unsigned int key;
int count;
struct HashMapNode *next;
} hash_map_node_t;
typedef struct HashMap
{
hash_map_node_t *nodes[HASH_MAP_SIZE];
} hash_map_t;
typedef struct LinkNode
{
char value;
struct LinkNode *next;
} lnode_t;
typedef struct CharStack
{
int length;
lnode_t *head;
} cstack_t;
unsigned int keygen(char *str)
{
unsigned int len=strlen(str);
return len * 31 + str[0] * 13 + str[len / 2] * 7 + str[len - 1] * 5;
}
unsigned int hash(unsigned int key)
{
return key % HASH_MAP_SIZE;
}
hash_map_node_t *hash_map_node_create()
{
hash_map_node_t *node = (hash_map_node_t *)malloc(sizeof(hash_map_node_t));
memset(node, 0, sizeof(hash_map_node_t));
return node;
}
hash_map_t *hash_map_create()
{
hash_map_t *map = (hash_map_t *)malloc(sizeof(hash_map_t));
memset(map, 0, sizeof(hash_map_t));
return map;
}
void hash_map_insert(hash_map_t *map, int key)
{
unsigned int index = hash(key);
hash_map_node_t *newEntry = map->nodes[index];
// Look for existing entry.
while (newEntry != NULL)
{
if (newEntry->key == key)
{
newEntry->count++;
return; // Entry found.
}
newEntry = newEntry->next;
}
// Create new entry.
newEntry = hash_map_node_create();
newEntry->key = key;
newEntry->count = 1;
// Insert new entry to top of chain.
newEntry->next = map->nodes[index];
map->nodes[index] = newEntry;
}
int hash_map_query(hash_map_t *map, int key)
{
unsigned int index = hash(key);
hash_map_node_t *entry = map->nodes[index];
while (entry != NULL)
{
if (entry->key == key)
{
return entry->count;
}
entry = entry->next;
}
// Not found.
return 0;
}
void hash_map_destroy(hash_map_t *map)
{
// Free nodes.
for (int i = 0; i < HASH_MAP_SIZE; ++i)
{
hash_map_node_t *entry = map->nodes[i];
if (entry == NULL)
{
continue;
}
while (entry->next != NULL)
{
hash_map_node_t *prev_entry = entry;
entry = entry->next;
free(prev_entry);
}
free(entry);
}
}
void lnode_init(lnode_t **tgt)
{
*tgt = (lnode_t *)malloc(sizeof(lnode_t));
(*tgt)->value = 0;
(*tgt)->next = NULL;
}
void cstack_init(cstack_t **tgt)
{
*tgt = (cstack_t *)malloc(sizeof(cstack_t));
(*tgt)->head = NULL;
(*tgt)->length = 0;
}
void cstack_push(cstack_t *tgt, char c)
{
lnode_t *node;
lnode_init(&node);
if (tgt->head == NULL)
{
node->next = NULL;
tgt->head = node;
}
else
{
node->next = tgt->head;
tgt->head = node;
}
tgt->length++;
tgt->head->value = c;
}
char cstack_pop(cstack_t *tgt)
{
if (tgt->head != NULL)
{
char c = tgt->head->value;
lnode_t *head = tgt->head;
tgt->head = tgt->head->next;
free(head);
return c;
}
else
{
return '0';
}
}
void cstack_destroy(cstack_t *tgt)
{
while (tgt->head != NULL)
{
lnode_t *head = tgt->head;
tgt->head = tgt->head->next;
free(head);
}
free(tgt);
}
int is_palindormic(char *str)
{
int len = strlen(str);
if (len == 0)
{
perror("Empty string.");
return -1;
}
else if (len == 1)
{
return 1;
}
else
{
cstack_t *letters;
cstack_init(&letters);
int i = 0;
for (; i < len / 2; ++i)
{
cstack_push(letters, str[i]);
}
if (len / 2 * 2 != len)
{
++i;
}
for (; i < len; ++i)
{
if (cstack_pop(letters) != str[i])
{
cstack_destroy(letters);
return 0;
}
}
cstack_destroy(letters);
return 1;
}
}
int palindormic_count(char *str)
{
int len = strlen(str);
char temp_str[11];
int count = 0;
hash_map_t *pd_map = hash_map_create();
for (size_t i = 1; i <= len; ++i)
{
// Iterate through every string length.
for (int j = 0; j <= len - i; ++j)
{
// Iterate through every substring of length i.
memcpy(temp_str, str + j, i);
// Null terminate string.
temp_str[i] = '\0';
if (hash_map_query(pd_map, keygen(temp_str)))
{
++count;
}
else if (is_palindormic(temp_str))
{
hash_map_insert(pd_map, keygen(temp_str));
++count;
}
}
}
hash_map_destroy(pd_map);
return count;
}
int main(void)
{
// Switch to fully buffered stdout.
setvbuf(stdout, NULL, _IOFBF, 0);
int n;
char string[11];
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%s", string);
printf("%d\n", palindormic_count(string));
}
return 0;
}