about summary refs log tree commit diff
path: root/codechef/binadd.c
blob: 8a2f56f8e6c408cef8c98b2baef76015c7df4384 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>

#define SIZE (100001 * sizeof(int))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

int main()
{
	size_t t;
	int *a = malloc(SIZE), *b = malloc(SIZE), c;
	*a = *b = 0;
	scanf("%zu", &t);

	while (t--) {
		size_t lena = 1, lenb = 1;
		for (scanf(" "); (c = getchar()) > 42; a[lena++] = c - 48);
		for (scanf(" "); (c = getchar()) > 42; b[lenb++] = c - 48);

		size_t len = (lena > lenb) ? lena : lenb;
		for (int *n = a + len, *e = a + lena, *p = b + lenb;
		     n-- > a; *n = (e-- > a && *e) << 1 | (p-- > b && *p));

		int tmp = 0;
		size_t result = 0, sum = 0;
		for (int *n = a + len; n-- > a;)
			switch (*n | tmp) {
			case 1:
				result = MAX(result, 1);
				break;
			case 3:
				tmp = 4;
				break;
			case 4:
				result = MAX(result, sum + 2);
				sum = tmp = 0;
				break;
			case 5:
			case 6:
				sum++;
				break;
			case 7:
				if (n[1] < 3) {
					result = MAX(result, sum + 2);
					sum = 0;
				}
			}
		printf("%zu\n", result);
	}

	return 0;
}