Codeforces 1181C

C. Flag
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in nmn⋅m colored pieces that form a rectangle with nn rows and mm columns.

The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.

Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different.

Codeforces 1181C_第1张图片    Codeforces 1181C_第2张图片

These subrectangles are flags.

Codeforces 1181C_第3张图片  Codeforces 1181C_第4张图片  Codeforces 1181C_第5张图片  Codeforces 1181C_第6张图片  Codeforces 1181C_第7张图片  Codeforces 1181C_第8张图片

These subrectangles are not flags.

Input

The first line contains two integers nn and mm (1n,m10001≤n,m≤1000) — the number of rows and the number of columns on the blanket.

Each of the next nn lines contains mm lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.

Output

In the only line print the number of subrectangles which form valid flags.

Examples
input
Copy
4 3
aaa
bbb
ccb
ddd
output
Copy
6
input
Copy
6 1
a
a
b
b
c
c
output
Copy
1
Note
Codeforces 1181C_第9张图片  Codeforces 1181C_第10张图片

The selected subrectangles are flags in the first example.

 

 

#include
#include
#define maxn 1010
using namespace std;
int b[maxn],cnt,n,m,ans,sum,len;
char a[maxn][maxn];
int main(){
    freopen("Cola.txt","r",stdin);
    freopen("2.txt","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            int ii=0,jj=0;
            cnt=0;sum=0;len=0;
            for(ii=i;ii<=n;ii++){
                if(a[ii][j]==a[i][j]){
                    b[++cnt]=a[i][j];
                    len++;
                }
                else break;
            }
            if(ii<=n)b[++cnt]=a[ii][j];
            else continue;
            ii++;
            for(;ii<=n;ii++){
                if(a[ii][j]==b[len+1]){
                    b[++cnt]=a[ii][j];
                }
                else break;
            }
            if(cnt!=len*2)continue;
            if(ii<=n)b[++cnt]=a[ii][j];
            else continue;
            ii++;
            for(;ii<=n;ii++){
                if(cnt==len*3)break;
                if(a[ii][j]==b[len*2+1]){
                    b[++cnt]=a[ii][j];
                }
                else break;
            }
            if(cnt!=len*3)continue;
            for(jj=j;jj<=m;jj++){
                bool flag1=0;
                for(ii=i;ii<=i+len*3-1;ii++){
                    if(a[ii][j]!=a[ii][jj]){
                        flag1=1;
                        break;
                    }
                }
                if(!flag1){
                    sum++;
                    ans+=sum;
                }
                else break;
            }
            j=jj-1;
        }
    }
    printf("%d\n",ans);
    return 0;
} 

 

你可能感兴趣的:(Codeforces 1181C)