@@ -44,7 +44,7 @@ void printer(NODE *head)
44
44
printf ("Fim da Lista!\n" );
45
45
}
46
46
47
- NODE * search (NODE * head , int item )
47
+ NODE * search (NODE * head , char item )
48
48
{
49
49
// printf("to dentro do search\n");
50
50
while (head != NULL )
@@ -59,58 +59,130 @@ NODE *search(NODE *head, int item)
59
59
return NULL ;
60
60
}
61
61
62
- int search_2 (NODE * head , int item )//função para procurar a melhor sequencia
62
+ int search_2 (NODE * head , char item )//função para procurar a melhor sequencia
63
63
{
64
+ DEBUG printf ("######### Procurando por: [%c] ##########\n" , item );
64
65
int count = 0 ;
65
66
while (head != NULL )
66
67
{
68
+ DEBUG printf ("head->item = [%c]\t" ,head -> item );
67
69
if (head -> item == item )
68
70
{
69
- // printf("ENCONTREI MAIS UM!!! [%d]\n", item );
71
+ DEBUG printf ("checagem: [%d]\n" ,count );
70
72
return count ; //retorna o endereço que contém o item
71
73
}
74
+
72
75
head = head -> next ;
73
- count ++ ;
76
+ count = count + 1 ;
77
+ DEBUG printf ("voltas: [%d]\n" ,count );
74
78
}
75
79
return 0 ;
76
80
}
77
81
78
- void func (NODE * head , int size , int beg , int end )
82
+ int search_3 (NODE * head , char item ) //devolve a contagem até o fim, nao importa oq for
79
83
{
80
- NODE * first_zero = search (head ,0 );//recebo o endereço do primeiro zero.
81
- beg = search_2 (head ,0 );
82
- int size1 = search_2 (first_zero ,1 ); //a partir do endereço do primeiro zero, procuro o proximo 1 e recebo o tamanho deste intervalo
83
- end = size1
84
- if (first_zero == NULL )//break
84
+ //DEBUG printf("######### Procurando por: [%c] ##########\n", item);
85
+ int count = 0 ;
86
+ while (head != NULL )
85
87
{
86
- printf ("0 0\n" );
87
- return ;
88
+ //DEBUG printf("head->item = [%c]\t",head->item);
89
+ if (head -> item == item )
90
+ {
91
+ count ++ ;
92
+ //DEBUG printf("ENCONTREI MAIS UM!!! [%d]\n", item);
93
+ //DEBUG printf("voltas: [%d]\n",count);
94
+ return count ; //retorna o endereço que contém o item
95
+ }
96
+ head = head -> next ;
97
+ count ++ ;
98
+ //DEBUG printf("voltas: [%d]\n",count);
88
99
}
89
- NODE * first_one = search ( first_zero , 1 ) ;
100
+ return count ;
90
101
}
91
- int main ()
102
+ void func ( NODE * head , int size , int beg , int end ) //beg e end são o começo e o fim da melhor sequencia
92
103
{
93
- char input [MAX ];
94
- scanf ("%s" , input );
95
- int q = strlen (input );
96
- NODE * lista1 = create_list (); //nó que aponta para nulo
104
+ NODE * first_zero = search (head ,'0' );//recebo o endereço do primeiro zero.
105
+ int pos_inicial = search_2 (head ,'0' )+ beg ;//recebe a posição do primeiro zero
106
+ DEBUG printf ("\tComeço encontrado: [%d]\n" ,pos_inicial );
97
107
98
- for (int i = 0 ; i < q ; i ++ )
108
+ NODE * first_one = search (first_zero ,'1' ); //will be my new head
109
+ int size1 = search_2 (first_zero , '1' );
110
+ int pos_final = size1 + pos_inicial - 1 ; //posição final do primeiro intervalo
111
+ // if(first_one == NULL)
112
+ // {
113
+ // pos_final = pos_inicial + search_3(first_zero,'1')-1;
114
+ // size1 = pos_final - pos_inicial;
115
+ // }
116
+ // else
117
+ // {
118
+ // size1 = search_2(first_zero,'1')-1; //a partir do endereço do primeiro zero, procuro o proximo 1 e recebo o tamanho deste intervalo
119
+ // pos_final = pos_inicial+size1-1;//end é a posição final
120
+ // }
121
+ DEBUG printf ("\tFim encontrado: [%d]\n" ,pos_final );
122
+ DEBUG printf ("TAMANHO: [%d]\n" ,size1 );
123
+
124
+ if (first_zero == NULL || first_one == NULL )//break
99
125
{
100
- lista1 = add (lista1 , input [i ]);
101
-
126
+ if (first_zero == NULL )
127
+ {
128
+ printf ("%d %d\n" ,pos_inicial ,pos_final );
129
+ }
130
+ printf ("%d %d\n" ,pos_inicial , pos_final );
131
+ return ;
102
132
}
133
+
134
+ if (size1 > size )
135
+ {
136
+ DEBUG printf ("\n------- FUNC 1 --------\n" );
137
+ func (first_one , size1 , pos_inicial , pos_final );
138
+ }
139
+ else {
140
+ DEBUG printf ("\n------- FUNC 2 --------\n\n" );
141
+ func (first_zero ,size ,beg ,end );
142
+ }
143
+
144
+ }
145
+ // void func2 (NODE* head)
146
+ // {
147
+ // int size;
148
+ // int pos = 0; //posição da iteração
149
+ // int beg = 0;
150
+ // int end = 0;
103
151
104
- func (lista1 );
152
+ // int state = -1;
153
+
154
+ // NODE* temp;
155
+ // while (head != NULL)
156
+ // {
157
+ // if(head->item == '0')
158
+ // {
159
+ // state = 0;
160
+ // beg = pos;
161
+ // }
162
+ // if(head->next->item == '1')
163
+ // {
164
+
165
+ // }
166
+
167
+ // pos++
168
+ // }
169
+
170
+ // }
171
+ int main ()
172
+ {
173
+ char input [MAX ];
174
+ NODE * lista1 = create_list (); //nó que aponta para nulo
105
175
do
106
176
{
107
177
scanf ("%s" , input );
108
178
int q = strlen (input );
109
-
110
- for (int i = 0 ; i < q ; i ++ )
179
+ if ( q == 1 && input [ 0 ] == '0' ) break ;
180
+ for (int i = q - 1 ; i >= 0 ; i -- )
111
181
{
112
- lista1 = add (lista1 , input [i ]);
182
+ //DEBUG printf("Adicionado [%c].\n",input[i]);
183
+ lista1 = add (lista1 , input [i ]);
113
184
}
185
+ func (lista1 , 0 ,0 ,0 );
114
186
} while (1 );
115
187
116
188
0 commit comments