import java.util.Random;
public class Subrectangle {
public int minMissed
(String S
) { int N = S.length();
boolean[] isBlack = new boolean[N];
for(int i = 0; i < N; ++i)
isBlack[i] = S.charAt(i) == '#';
int first_black = 0;
while(first_black < N && !isBlack[first_black]) ++first_black;
if(first_black == N) return 1;
int last_black = N - 1;
while(!isBlack[last_black]) --last_black;
int answer = -1;
for(int b = 1; b <= N; ++b)
for(int w = 0; w <= N; ++w) {
int added = 0;
for(int i = first_black; i <= last_black;) {
if(w == 0 && !isBlack[i]) {
added = -1;
break;
}
int need = b;
while(need > 0 && i < N && isBlack[i]) {
--need;
++i;
}
added += need;
if(i > last_black) break;
need = w;
while(need > 0 && i < N && !isBlack[i]) {
--need;
++i;
}
added += need;
}
if(added == -1) continue;
int W = b + w;
int start = first_black % W;
if(start + b > W)
added += W - start;
int area = S.length() + added;
if(area % W != 0)
added += W - area % W;
if(answer == -1 || added < answer)
answer = added;
}
return answer;
}
int j = 0;
for(int i = 0; i < A.length(); ++i)
if(j < B.length() && A.charAt(i) == B.charAt(j))
++j;
return j == B.length();
}
// brute force
for(int area = 1; ; ++area)
for(int H = 1; H <= area; ++H) if(area % H == 0) {
int W = area / H;
for(int r1 = 0; r1 < H; ++r1)
for(int r2 = r1; r2 < H; ++r2)
for(int c1 = 0; c1 < W; ++c1)
for(int c2 = c1; c2 < W; ++c2) {
for(int row = 0; row < H; ++row)
for(int col = 0; col < W; ++col) {
if (r1 <= row && row <= r2 && c1 <= col && col <= c2)
A += '#';
else
A += '.';
}
if(containsSubsequence(A, S))
return area - S.length();
}
}
// unreachable
}
}