題目:MIN-MEX Cut
由於觀察 3、4兩點 ,推斷用窮舉的方式應該不會太複雜,分別舉出 s[0] == '0'
跟 s[0] == '1'
的情況:
s |minimal sum of MEX
---+----
0| 1
01| 1
010| 2
0101| 2
01010| 2
010101| 2
0101010| 2
s |minimal sum of MEX
---+----
1| 0
10| 1
101| 1
1010| 2
10101| 2
101010| 2
1010101| 2
10101010| 2
依照這兩張表,把程式寫出來就好
// https://codeforces.com/contest/1566/problem/B
#include <stdio.h>
#define SIZE (int)1e5
int count_part(char *s)
{
int result = 1;
char *p = s;
while (*(++p) != '\0')
if (*p != *(p-1))
result++;
return result;
}
int main()
{
int t, parts;
scanf("%d\n", &t);
while (t--) {
char s[SIZE+1];
scanf("%s", s);
parts = count_part(s);
if (parts == 1) {
if (*s == '0')
printf("1");
else
printf("0");
} else if (parts == 2) {
printf("1");
} else if (parts == 3 && *s == '1') {
printf("1");
} else {
printf("2");
}
printf("\n");
}
return 0;
}