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;
}
codeforces 1181C相关推荐
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)
题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...
- 【codeforces 812C】Sagheer and Nubian Market
[题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...
- CodeForces 获得数据
针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...
- codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...
题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...
- CodeForces 595A
题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...
- codeforces A. Jeff and Digits 解题报告
题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
- Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...
最新文章
- 服务端主动推送数据,除了 WebSocket 你还能想到啥?
- QMainWindow多线程demo
- 使用blas做矩阵乘法
- 算法—详细讲解单向链表的实现(python)
- RabbitMQ(六)整合SpringBoot
- gulp + webpack 构建多页面前端项目 1
- HTML5中volume样式自定义,html5中关于volume属性的使用详解
- 64位汇编中的布尔指令
- 基础 | 这波编程基础绝了!快来学习!
- 计算机辅导资料,计算机基础辅导资料.doc
- 生活中的实验 —— 家庭电路
- 互联网晚报 | 7月10日 星期天 | 快手官宣:7月18日周杰伦独家直播;​400亿额度,秒光!7月总票房破10亿...
- C语言利用堆筛选前1000大元素
- 密室逃脱全集(试试吧)
- C++ 创建桌面快捷方式
- XCode 动态库未签名问题的解决
- 大数据技术与应用实训心得_大数据心得体会
- win7系统如何设置远程桌面连接【系统天地】
- Bit-Z的投资忠告
- 芸赞通天下沈杨——怀揣梦想,不断努力