Skip to content

Commit b78a19b

Browse files
committed
started shinobus's almost done with big_seq_v2
1 parent 3a1af6f commit b78a19b

File tree

5 files changed

+330
-42
lines changed

5 files changed

+330
-42
lines changed

biggest_seq_v2.c

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
#define DEBUG if(1)
6+
#define MAX 100001
7+
/*
8+
01100010
9+
10000001
10+
11111111
11+
00000000
12+
10101010
13+
0
14+
*/
15+
typedef struct node
16+
{
17+
char item;
18+
int pos;
19+
struct node* next;
20+
}NODE;
21+
22+
typedef struct head
23+
{
24+
int size;
25+
NODE* head;
26+
}LIST;
27+
28+
LIST* initList()
29+
{
30+
LIST* new_list = (LIST*) malloc(sizeof(LIST));
31+
new_list->head = NULL;
32+
new_list->size = 0;
33+
return new_list;
34+
}
35+
36+
int is_empty(NODE *head)
37+
{
38+
return (head == NULL);
39+
}
40+
41+
void add(LIST *head, char item, int posi)
42+
{
43+
NODE *new_node = (NODE *)malloc(sizeof(NODE));
44+
new_node->item = item;
45+
new_node->next = head->head;
46+
47+
new_node->pos = posi;
48+
49+
head->head = new_node;
50+
head->size++;
51+
//DEBUG printf("adicionado[[[%d]]]\t",new_node->item);
52+
//DEBUG printf("Posição adicionada: %d\n",new_node->pos);
53+
return;
54+
}
55+
NODE* search(NODE* list, char target)
56+
{
57+
NODE* tmp = list;
58+
while (tmp != NULL)
59+
{
60+
if(tmp->item == target)
61+
{
62+
return tmp;
63+
}
64+
tmp = tmp->next;
65+
}
66+
return NULL;
67+
}
68+
69+
void bestSeq(LIST* list, int len)
70+
{
71+
NODE* tmp = list->head;
72+
int size = 0;
73+
int pos1 = 0;
74+
int pos2 = 0;
75+
int Bpos1 = 0;
76+
int Bpos2 = 0;
77+
int Bsize = 0;
78+
int flag = 0;
79+
80+
do
81+
{
82+
tmp = search(tmp,'0');
83+
if (tmp == NULL) break;
84+
else pos1 = tmp->pos;
85+
DEBUG printf("pos1 = [%d]\t",pos1);
86+
87+
88+
tmp = search(tmp,'1');
89+
if (tmp == NULL)
90+
{
91+
pos2 = len-1;
92+
// size = pos2 - pos1;
93+
// if (size > Bsize)
94+
// {
95+
// Bsize = size;
96+
// Bpos1 = pos1;
97+
// Bpos2 = pos2;
98+
// }
99+
// if (size == Bsize && flag == 0)
100+
// {
101+
// Bpos1 = pos1;
102+
// Bpos2 = pos2;
103+
// flag = 1;
104+
DEBUG printf("----------------:\n");
105+
// }
106+
break;
107+
}
108+
else pos2 = tmp->pos-1;
109+
DEBUG printf("tmp->pos = [%d] ", tmp->pos);
110+
DEBUG printf("pos2 = [%d]\t",pos2);
111+
if(pos2<0) break;
112+
113+
size = pos2 - pos1;
114+
DEBUG printf("size = [%d]\n", size);
115+
if (size > Bsize)
116+
{
117+
Bsize = size;
118+
Bpos1 = pos1;
119+
Bpos2 = pos2;
120+
}
121+
if (size == Bsize && flag == 0)
122+
{
123+
Bpos1 = pos1;
124+
Bpos2 = pos2;
125+
flag = 1;
126+
DEBUG printf("este caso:\n");
127+
}
128+
else if (size == Bsize)
129+
{
130+
continue;
131+
}
132+
133+
134+
135+
136+
tmp = tmp->next;
137+
} while (1);
138+
139+
printf("%d %d\n", Bpos1, Bpos2);
140+
return;
141+
}
142+
void printer(LIST *list)
143+
{
144+
NODE* head = list->head;
145+
if (head == NULL)
146+
{
147+
printf("Lista vazia!\n");
148+
return;
149+
}
150+
while (head != NULL)
151+
{
152+
printf("[%c]", head->item);
153+
head = head->next;
154+
}
155+
printf("\nFim da Lista!\n");
156+
return;
157+
}
158+
void inverter (LIST* stack)
159+
{
160+
NODE* current = stack->head;
161+
NODE* next = NULL;
162+
NODE* prev = NULL;
163+
while(current != NULL)
164+
{
165+
next = current->next;
166+
current->next = prev;
167+
prev = current;
168+
current = next;
169+
}
170+
stack->head = prev;
171+
return;
172+
}
173+
void delete (NODE *head)
174+
{
175+
NODE* p, *store;
176+
p = head;
177+
while (p != NULL)
178+
{
179+
store = p->next;
180+
free(p);
181+
p=store;
182+
}
183+
184+
}
185+
186+
int main()
187+
{
188+
char input[MAX] = {};
189+
LIST *lista1 = initList();
190+
int q = 0;
191+
DEBUG printf("começou a lista\n");
192+
do
193+
{
194+
scanf("%s", input);
195+
q = strlen(input);
196+
DEBUG printf("[%s]\n",input);
197+
if(q == 1 && input[0] == '0') break;
198+
for (int i = q-1; i >= 0 ; i--)
199+
{
200+
//DEBUG printf("Adicionado [%c].\n",input[i]);
201+
if(input[i] == '\n') continue;
202+
add(lista1, input[i],i);
203+
}
204+
bestSeq(lista1, q);
205+
//delete(lista1->head);
206+
DEBUG printf("Sucesso!\n\n");
207+
} while (1);
208+
//inverter(lista1);
209+
//printer(lista1);
210+
211+
212+
return 0;
213+
}

intersection.c

+21-31
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ typedef struct head
1313
{
1414
int size;
1515
NODE* head;
16-
NODE* tail;
1716
}LIST;
1817

1918
LIST* init_list()
@@ -70,7 +69,8 @@ bool search(LIST* list, int target)
7069
if(tmp->data == target)
7170
{
7271
return true;
73-
}
72+
}
73+
tmp = tmp->next;
7474
}
7575
return false;
7676
}
@@ -82,29 +82,15 @@ bool comparator(int* a, int* b, LIST* list) //gets two arrays and adds to the li
8282
int tmp;
8383
int t = 0;
8484
int last = 468468486;
85-
DEBUG printf("OI\n");
8685
for (int i = 0; i < count; i++)
8786
{
8887
tmp = a[i];
89-
//DEBUG printf("###### a[%d] = %d #######\n",i, a[i]);
9088
for (int j = 0; j < count; j++)
9189
{
92-
//DEBUG printf("tmp = %d e b[%d] = %d\n", tmp, j, b[j]);
90+
DEBUG printf("OI [%d]\n", j);
9391
if (tmp == b[j])
9492
{
95-
//DEBUG printf("Elemento [%d] adicionado.\n", tmp);
96-
/* if (search(list, tmp))
97-
{
98-
continue;
99-
}
100-
add(list, tmp);
101-
102-
*/
103-
if (tmp == last)
104-
{
105-
continue;
106-
}
107-
93+
if (tmp == last) continue;
10894
if(i != 0 && tmp == last) continue;
10995
sort[t] = tmp;
11096
last = tmp;
@@ -113,20 +99,19 @@ bool comparator(int* a, int* b, LIST* list) //gets two arrays and adds to the li
11399
}
114100
}
115101
}
116-
117-
//qsort(sort,t,sizeof(int), compar);
102+
DEBUG printf("XAU\n");
103+
if (flag == 0) return false;
118104
for (int i = 0; i < t; i++)
119105
{
106+
tmp = sort[i];
107+
DEBUG printf("tMP = [%d]\n", tmp);
108+
if (search(list, tmp) && i != 0)
109+
{
110+
continue;
111+
}
120112
add(list,sort[i]);
121113
}
122-
123-
if (flag > 0)
124-
{
125-
return true;
126-
}
127-
return false;
128-
129-
114+
return true;
130115
}
131116
void printer(LIST* head)
132117
{
@@ -148,15 +133,20 @@ int main()
148133
for (int i = 0; i < count; i++)
149134
{
150135
scanf("%d",&n);
151-
a[i] = n;
136+
a[i] = n;
137+
DEBUG printf("a[%d] = %d\n",i,n);
152138
}
153139
for (int i = 0; i < count; i++)
154140
{
155141
scanf("%d",&n);
156-
b[i] = n;
142+
b[i] = n;
143+
DEBUG printf("b[%d] = %d\n",i,n);
157144
}
145+
DEBUG printf("CADE CADE VAI PRO COMPARADOR OU NAO\n");
158146
n = comparator(a,b,head);
147+
if(!n) {printf("VAZIO\n");return 0;}
159148
bubbleSort(head);
160149
if (n) printer(head);
161-
else printf("VAZIO\n");
150+
else
151+
return 0;
162152
}

0 commit comments

Comments
 (0)