hdu 2870 Largest Submatrixhdu
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870
题意:一个矩形由字符a、b、c、w、x、y、z组成,其中w可以代替a、b,x可以代替b、c,y可以代替a、c,z可以代替a、b、c。求由相同组成的最大内矩形面积。
思路:方法与hdu 1505一样,不过需要开三个数组a[]、b[]、c[]保存相同字符的区域。
1 #include <cstdio>
2 #include <cstring>
3 #define N 1005
4
5 int a[N][N], b[N][N], c[N][N], ha[N][N], hb[N][N], hc[N][N], l[N], r[N];
6 char ss[N];
7 int max2(int x, int y)
8 {
9 return x > y ? x : y;
10 }
11
12 int main()
13 {
14 int n, m;
15 while(scanf("%d%d",&n, &m)!=EOF)
16 {
17 memset(a, 0, sizeof(a));
18 memset(b, 0, sizeof(b));
19 memset(c, 0, sizeof(c));
20 memset(ha, 0, sizeof(ha));
21 memset(hb, 0, sizeof(hb));
22 memset(hc, 0, sizeof(hc));
23 for(int i=1; i<=n; i++)
24 {
25 scanf("%s",ss+1);
26 for(int j=1; j<=m; j++)
27 {
28 if(ss[j]=='a')
29 a[i][j] = 1;
30 else if(ss[j]=='b')
31 b[i][j] = 1;
32 else if(ss[j]=='c')
33 c[i][j] = 1;
34 else if(ss[j]=='w')
35 {
36 a[i][j] = 1;
37 b[i][j] = 1;
38 }
39 else if(ss[j]=='x')
40 {
41 b[i][j] = 1;
42 c[i][j] = 1;
43 }
44 else if(ss[j]=='y')
45 {
46 a[i][j] = 1;
47 c[i][j] = 1;
48 }
49 else if(ss[j]=='z')
50 {
51 a[i][j] = 1;
52 b[i][j] = 1;
53 c[i][j] = 1;
54 }
55 }
56 }
57
58
59 //a
60 int ans = 0;
61 for(int i=1; i<=n; i++)
62 {
63 for(int j=1; j<=m; j++)
64 {
65 l[j] = r[j] = j;
66 if(a[i][j]==1) ha[i][j] = ha[i-1][j] + 1;
67 }
68 for(int j=1; j<=m; j++)
69 {
70 while(l[j]-1>=1 && ha[i][l[j]-1]>=ha[i][j])
71 l[j] = l[l[j]-1];
72 }
73 for(int j=m-1; j>=1; j--)
74 {
75 while(r[j]+1<=m && ha[i][r[j]+1]>=ha[i][j])
76 r[j] = r[r[j]+1];
77 }
78 for(int j=1; j<=m; j++)
79 ans = max2(ans, (r[j]-l[j]+1)*ha[i][j]);
80 //b
81 for(int j=1; j<=m; j++)
82 {
83 l[j] = r[j] = j;
84 if(b[i][j]==1) hb[i][j] = hb[i-1][j] + 1;
85 }
86 for(int j=1; j<=m; j++)
87 {
88 while(l[j]-1>=1 && hb[i][l[j]-1]>=hb[i][j])
89 l[j] = l[l[j]-1];
90 }
91 for(int j=m-1; j>=1; j--)
92 {
93 while(r[j]+1<=m && hb[i][r[j]+1]>=hb[i][j])
94 r[j] = r[r[j]+1];
95 }
96 for(int j=1; j<=m; j++)
97 ans = max2(ans, (r[j]-l[j]+1)*hb[i][j]);
98 //c
99 for(int j=1; j<=m; j++)
100 {
101 l[j] = r[j] = j;
102 if(c[i][j]==1) hc[i][j] = hc[i-1][j] + 1;
103 }
104 for(int j=1; j<=m; j++)
105 {
106 while(l[j]-1>=1 && hc[i][l[j]-1]>=hc[i][j])
107 l[j] = l[l[j]-1];
108 }
109 for(int j=m-1; j>=1; j--)
110 {
111 while(r[j]+1<=m && hc[i][r[j]+1]>=hc[i][j])
112 r[j] = r[r[j]+1];
113 }
114 for(int j=1; j<=m; j++)
115 ans = max2(ans, (r[j]-l[j]+1)*hc[i][j]);
116 }
117 printf("%d\n",ans);
118 }
119 return 0;
120 }
2 #include <cstring>
3 #define N 1005
4
5 int a[N][N], b[N][N], c[N][N], ha[N][N], hb[N][N], hc[N][N], l[N], r[N];
6 char ss[N];
7 int max2(int x, int y)
8 {
9 return x > y ? x : y;
10 }
11
12 int main()
13 {
14 int n, m;
15 while(scanf("%d%d",&n, &m)!=EOF)
16 {
17 memset(a, 0, sizeof(a));
18 memset(b, 0, sizeof(b));
19 memset(c, 0, sizeof(c));
20 memset(ha, 0, sizeof(ha));
21 memset(hb, 0, sizeof(hb));
22 memset(hc, 0, sizeof(hc));
23 for(int i=1; i<=n; i++)
24 {
25 scanf("%s",ss+1);
26 for(int j=1; j<=m; j++)
27 {
28 if(ss[j]=='a')
29 a[i][j] = 1;
30 else if(ss[j]=='b')
31 b[i][j] = 1;
32 else if(ss[j]=='c')
33 c[i][j] = 1;
34 else if(ss[j]=='w')
35 {
36 a[i][j] = 1;
37 b[i][j] = 1;
38 }
39 else if(ss[j]=='x')
40 {
41 b[i][j] = 1;
42 c[i][j] = 1;
43 }
44 else if(ss[j]=='y')
45 {
46 a[i][j] = 1;
47 c[i][j] = 1;
48 }
49 else if(ss[j]=='z')
50 {
51 a[i][j] = 1;
52 b[i][j] = 1;
53 c[i][j] = 1;
54 }
55 }
56 }
57
58
59 //a
60 int ans = 0;
61 for(int i=1; i<=n; i++)
62 {
63 for(int j=1; j<=m; j++)
64 {
65 l[j] = r[j] = j;
66 if(a[i][j]==1) ha[i][j] = ha[i-1][j] + 1;
67 }
68 for(int j=1; j<=m; j++)
69 {
70 while(l[j]-1>=1 && ha[i][l[j]-1]>=ha[i][j])
71 l[j] = l[l[j]-1];
72 }
73 for(int j=m-1; j>=1; j--)
74 {
75 while(r[j]+1<=m && ha[i][r[j]+1]>=ha[i][j])
76 r[j] = r[r[j]+1];
77 }
78 for(int j=1; j<=m; j++)
79 ans = max2(ans, (r[j]-l[j]+1)*ha[i][j]);
80 //b
81 for(int j=1; j<=m; j++)
82 {
83 l[j] = r[j] = j;
84 if(b[i][j]==1) hb[i][j] = hb[i-1][j] + 1;
85 }
86 for(int j=1; j<=m; j++)
87 {
88 while(l[j]-1>=1 && hb[i][l[j]-1]>=hb[i][j])
89 l[j] = l[l[j]-1];
90 }
91 for(int j=m-1; j>=1; j--)
92 {
93 while(r[j]+1<=m && hb[i][r[j]+1]>=hb[i][j])
94 r[j] = r[r[j]+1];
95 }
96 for(int j=1; j<=m; j++)
97 ans = max2(ans, (r[j]-l[j]+1)*hb[i][j]);
98 //c
99 for(int j=1; j<=m; j++)
100 {
101 l[j] = r[j] = j;
102 if(c[i][j]==1) hc[i][j] = hc[i-1][j] + 1;
103 }
104 for(int j=1; j<=m; j++)
105 {
106 while(l[j]-1>=1 && hc[i][l[j]-1]>=hc[i][j])
107 l[j] = l[l[j]-1];
108 }
109 for(int j=m-1; j>=1; j--)
110 {
111 while(r[j]+1<=m && hc[i][r[j]+1]>=hc[i][j])
112 r[j] = r[r[j]+1];
113 }
114 for(int j=1; j<=m; j++)
115 ans = max2(ans, (r[j]-l[j]+1)*hc[i][j]);
116 }
117 printf("%d\n",ans);
118 }
119 return 0;
120 }