codeforces 1181C

原题http://codeforces.com/problemset/problem/1181/C

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 n⋅mn⋅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.

  

These subrectangles are flags.

     

These subrectangles are not flags.

Input

The first line contains two integers nn and mm (1≤n,m≤10001≤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

 

The selected subrectangles are flags in the first example.

题意:给你一块n*m的矩形,需要裁剪成一块三色条纹的子矩形,并且三色条纹要等高,宽度不做限制,问有多少种不同的子矩形可以选择。

思路:遍历找到一条符合条件h*1的子矩形,然后向后遍历,找到w个相同的子矩形,在h*w的矩形中有1-w的累计和S中方法裁剪,遍历找到所有S,相加;

 

#include<iostream>
#include<queue>
#include<cstring> 
#include<cmath>
#include<map>
#include<algorithm>
#define up(i,x,y) for(i=x;i<=y;i++)  
#define down(i,x,y) for(i=x;i>=y;i--)  
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a<b?a:b
#define MAX(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))
#define MIN(a,b,c) (a<b?(a<c?a:c):(b<c?b:c))
using namespace std;

typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
const int MAXN = 2750131;
ll T,n,m,k,i,j;
ll gcd(ll p,ll q)
{return q==0?p:gcd(q,p%q);}

int main()
{
	string a[1005];
	string b;
	int sumb[5],ans=0;
	cin>>n>>m;
	for(i=0;i<n;i++){
		cin>>a[i];
	}

	
	int k=0,ii=0,jj=0;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			int flag=0;
			sumb[0]=0;
			sumb[1]=0;
			sumb[2]=0;
			b="";
			b+=a[i][j];
		//	cout<<i<<" "<<j<<" "<<b<<endl;
			ii=i;
			while(a[ii][j]==b[0]&&ii<n){
				sumb[0]++;
				ii++;
			}
			if(ii<n){
				b+=a[ii][j];
				while(a[ii][j]==b[1]&&ii<n){
					sumb[1]++;
					ii++;
				}
			}
			
			if(sumb[0]!=sumb[1]){
				continue;
			}
			if(ii<n){
				b+=a[ii][j];
				while(a[ii][j]==b[2]&&ii<n){
					sumb[2]++;
					ii++;
					if(sumb[2]==sumb[0]){
						flag=1;break;
					}
				}
			}
		//	cout<<b<<endl;
		//	cout<<sumb[0]<<" "<<sumb[1]<<" "<<sumb[2]<<endl;
			if(flag){
				
				int sum=1;
				ans+=sum;
				for(jj=j+1;jj<m;jj++){
					int flag1=1;
					for(int is=i;is<ii;is++){
						if(a[is][jj]!=a[is][jj-1]){
							flag1=0;break;
						}
					}
					if(flag1){
						sum++;
						ans+=sum;
					}
					else{
						break;
					}
				}
				//cout<<i<<" "<<j<<" "<<sum<<endl; 
				j=jj-1;
			}
			
		}
	}
	cout<<ans<<endl;
	return 0;	
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值