UVA 836 - Largest Submatrix ( DP, Largest 1s Submatrix )

UVA 836 - Largest Submatrix ( DP, Largest 1s Submatrix )

///==================================================///
/// HELLO WORLD !! ///
/// IT'S ME ///
/// BISHAL GAUTAM ///
/// [ bsal.gautam16@gmail.com ] ///
///==================================================///
#include<bits/stdc++.h>
#define X first
#define Y second
#define mpp make_pair
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
///---------------------
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%lld",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
///------------------------------------
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define fr(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
typedef long long ll;

/// Digits 0123456789012345678 ///
#define MX 102
#define inf 1000000010
#define MD 1000000007LL
#define eps 1e-9
///===============================///

int n,dp[MX][MX];
char s[MX][MX];
int main() {
int tc,i,j,k,nw=0;
S(tc);
while(tc--) {
if(nw)printf("\n"); nw=1;
scanf("%s",s[1]);
n=strlen(s[1]);
for(i=2;i<=n;i++){
scanf("%s",s[i]);
}
for(i=1;i<=n;i++){
dp[i][0]=0;
for(j=1;j<=n;j++){
dp[i][j]=(dp[i][j-1]+(s[i][j-1]=='1'));
}
}

int mx=0;
for(i=1; i<=n; i++) { ///First column;
for(j=i; j<=n; j++) { ///Second column
int mxx=0;
for(k=1; k<=n; k++) {
int cnt=0; ///Taking consecutively till valid
while( k<=n && ( dp[k][j]-dp[k][i-1]>=(j-i+1) ) ){
cnt++;
k++;
}
mxx=max(mxx,cnt);
}
mx=max(mx,mxx*(j-i+1));
}
}
printf("%d\n",mx);
}
return 0;
}

Comments

Popular posts from this blog

HackerEarth: City and Campers 2 (DSU-UnionFind)

Two Pointers Technique & Binary Search For Beginners

Problem : Codeforces Round #406 (Div. 1) B [ Legacy ]( Dijakstra, Segment tree)