-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiggest_seq_v2.c
213 lines (192 loc) · 4.03 KB
/
biggest_seq_v2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG if(1)
#define MAX 100001
/*
01100010
10000001
11111111
00000000
10101010
0
*/
typedef struct node
{
char item;
int pos;
struct node* next;
}NODE;
typedef struct head
{
int size;
NODE* head;
}LIST;
LIST* initList()
{
LIST* new_list = (LIST*) malloc(sizeof(LIST));
new_list->head = NULL;
new_list->size = 0;
return new_list;
}
int is_empty(NODE *head)
{
return (head == NULL);
}
void add(LIST *head, char item, int posi)
{
NODE *new_node = (NODE *)malloc(sizeof(NODE));
new_node->item = item;
new_node->next = head->head;
new_node->pos = posi;
head->head = new_node;
head->size++;
//DEBUG printf("adicionado[[[%d]]]\t",new_node->item);
//DEBUG printf("Posição adicionada: %d\n",new_node->pos);
return;
}
NODE* search(NODE* list, char target)
{
NODE* tmp = list;
while (tmp != NULL)
{
if(tmp->item == target)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
void bestSeq(LIST* list, int len)
{
NODE* tmp = list->head;
int size = 0;
int pos1 = 0;
int pos2 = 0;
int Bpos1 = 0;
int Bpos2 = 0;
int Bsize = 0;
int flag = 0;
do
{
tmp = search(tmp,'0');
if (tmp == NULL) break;
else pos1 = tmp->pos;
DEBUG printf("pos1 = [%d]\t",pos1);
tmp = search(tmp,'1');
if (tmp == NULL)
{
pos2 = len-1;
// size = pos2 - pos1;
// if (size > Bsize)
// {
// Bsize = size;
// Bpos1 = pos1;
// Bpos2 = pos2;
// }
// if (size == Bsize && flag == 0)
// {
// Bpos1 = pos1;
// Bpos2 = pos2;
// flag = 1;
DEBUG printf("----------------:\n");
// }
break;
}
else pos2 = tmp->pos-1;
DEBUG printf("tmp->pos = [%d] ", tmp->pos);
DEBUG printf("pos2 = [%d]\t",pos2);
if(pos2<0) break;
size = pos2 - pos1;
DEBUG printf("size = [%d]\n", size);
if (size > Bsize)
{
Bsize = size;
Bpos1 = pos1;
Bpos2 = pos2;
}
if (size == Bsize && flag == 0)
{
Bpos1 = pos1;
Bpos2 = pos2;
flag = 1;
DEBUG printf("este caso:\n");
}
else if (size == Bsize)
{
continue;
}
tmp = tmp->next;
} while (1);
printf("%d %d\n", Bpos1, Bpos2);
return;
}
void printer(LIST *list)
{
NODE* head = list->head;
if (head == NULL)
{
printf("Lista vazia!\n");
return;
}
while (head != NULL)
{
printf("[%c]", head->item);
head = head->next;
}
printf("\nFim da Lista!\n");
return;
}
void inverter (LIST* stack)
{
NODE* current = stack->head;
NODE* next = NULL;
NODE* prev = NULL;
while(current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
stack->head = prev;
return;
}
void delete (NODE *head)
{
NODE* p, *store;
p = head;
while (p != NULL)
{
store = p->next;
free(p);
p=store;
}
}
int main()
{
char input[MAX] = {};
LIST *lista1 = initList();
int q = 0;
DEBUG printf("começou a lista\n");
do
{
scanf("%s", input);
q = strlen(input);
DEBUG printf("[%s]\n",input);
if(q == 1 && input[0] == '0') break;
for (int i = q-1; i >= 0 ; i--)
{
//DEBUG printf("Adicionado [%c].\n",input[i]);
if(input[i] == '\n') continue;
add(lista1, input[i],i);
}
bestSeq(lista1, q);
//delete(lista1->head);
DEBUG printf("Sucesso!\n\n");
} while (1);
//inverter(lista1);
//printer(lista1);
return 0;
}