https://codeforces.com/contest/1080/problem/E

题意

有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每一列都是一个回文串,问最多有多少个这样的子矩阵

思路

  • 首先可以思考如何暴力,枚举四个边界(子矩阵),然后判定一下子矩阵的每一行每一列,这样的复杂度是nmnmn*m
  • 很明显需要找一下规律来优化一下复杂度,
    1. 对于每一行来说,最多只能存在一种字母的数量是奇数,这样一定可以保证这一行是回文串
    2. 对于每一列来说,因为不能交换顺序,所以必须具有回文性质(关于中心点对称),这就需要要求每一行相同的字母数量应该相同
  • 根据第二个规律,因为只需要验证每一行是否相同,所以可以采用字符串hash每一行,O(n*m)处理,O(1)验证
  • 首先枚举列的两个端点 O(mm),然后o(n)找出所有连续的回文行,跑马拉车算法,求出连续的回文行中所有的回文子行,O(n),总复杂度O(mm*n)
  • 符合回文性质的都可以用马拉车求出最长回文字串,回文字串个数
#include<bits/stdc++.h>
#define ll long long
#define M 605
#define key 233
#define P 1000000007
using namespace std;
int n,m,i,j,a[M][M],c1,c2,l1,l2,msk[M],cnt[M][30],x,ans=0,d1[M],d2[M];
char s[M];
ll hs[M],p[M],tp[M]; int ck(int x){
return (x==0)||((x&(x-1))==0);
}
void init(){
p[0]=1;for(int i=1;i<=30;i++)p[i]=p[i-1]*key%P;
} int cal(int l1,int l2){
int n=0,ans=0;
for(int i=l1;i<=l2;i++)tp[++n]=hs[i];
int l=0,r=0,x;
for(int i=1;i<=n;i++){
if(i>r)x=1;
else x=min(d1[l+r-i],r-i);
while(i-x>=1&&i+x<=n&&tp[i-x]==tp[i+x])x++;
d1[i]=x;
ans+=d1[i];
if(i+x-1>r){r=i+x-1;l=i-x+1;}
}
l=r=0;
for(int i=1;i<=n;i++){
if(i>r)x=0;
else x=min(d2[l+r-i+1],r-i+1);
while(i-x-1>=1&&i+x<=n&&tp[i-x-1]==tp[i+x])x++;
d2[i]=x;
ans+=d2[i];
if(i+x>=r){l=i-x;r=i+x-1;}
}
return ans;
}
int main(){
init();
cin>>n>>m;
for(i=1;i<=n;i++){
scanf("%s",s+1);
for(j=1;j<=m;j++){
a[i][j]=s[j]-'a';
}
}
for(c1=1;c1<=m;c1++){
for(i=1;i<=n;i++){
hs[i]=0;msk[i]=0;
memset(cnt[i],0,sizeof(cnt[i]));
}
for(c2=c1;c2<=m;c2++){
for(i=1;i<=n;i++){
x=a[i][c2];
if(cnt[i][x]){
hs[i]-=cnt[i][x]*p[x+1]%P;
if(hs[i]<0)hs[i]+=P;
}
cnt[i][x]++;
hs[i]+=cnt[i][x]*p[x+1]%P;hs[i]%=P;
msk[i]^=(1<<x);
}
l1=1;
while(l1<=n){
if(!ck(msk[l1])){l1++;continue;}
l2=l1;
while(l2<=n&&ck(msk[l2]))l2++;
l2--;
ans+=cal(l1,l2);
l1=l2+1;
}
}
}
cout<<ans;
}

Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)的更多相关文章

  1. Codeforces Round #495 (Div. 2) D. Sonya and Matrix

    http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...

  2. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  3. Codeforces Round #524 (Div. 2) Solution

    A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...

  4. Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

  5. Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题

    C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...

  6. Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩

    题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...

  7. Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  8. Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]

    C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #495 (Div. 2) C. Sonya and Robots

    http://codeforces.com/contest/1004/problem/C 题意: 在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q.现在这两个机器 ...

随机推荐

  1. ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps(二分+线段树区间最小)

    During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...

  2. TZOJ 1911 A Plug for UNIX(最大流)

    描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...

  3. js常用的数组,,字符串,,Math..正则方法

    数组 slice[start,end] 返回从原数组中指定开始下标到结束下标之间的项目组成新数组(不会影响原数组) splice() 1.删除功能:2个参数 , 起始位置 , 删除的项目 2.插入功能 ...

  4. 畅谈Redis和Memcached的区别

    简述 memcached 和 redis 都很类似:都是内存型数据库,数据保存在内存中,通过tcp直接存取,优势是速度快,并发高,缺点是数据类型有限,查询功能不强,一般用作缓存. 那么题主说 memc ...

  5. Head First Servlets & JSP 学习笔记 第五章 —— 作为Web应用

    初始化参数:(init-param) 初始化参数写在web.xml文件中:(写在<servlet>标签内部) <servlet> <servlet-name>Bee ...

  6. xshell实时跟踪日志与中文乱码设置

    1.实时跟踪日志命令 tail -f logName.log 动态查看名为logName的日志信息 ctrl+c 退出实时跟踪 2.中文乱码设置 在Xshell.putty.SSH Secure Sh ...

  7. Hibernate: save, persist, update, merge, saveOrUpdate[z]

    [z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...

  8. openshift上传java web项目

    下载当前客户端 OC(Openshift Client) https://mirror.openshift.com/pub/openshift-v3/clients/3.9.14/windows/oc ...

  9. vue 自定义组件directives

    自定义指令:以v开头,如:v-mybind. 代码示例: <input v-mybind /> directives:{ mybind:{ bind:function (el) { el. ...

  10. threejs- z-fighting 问题(模型的重叠部位便不停的闪烁起来。这便是Z-Fighting问题)

    Z-Buffer 在threejs中,使用深度缓冲(Z-Buffer)来完成场景可见性计算,即确定场景哪部分可见,哪部分不可见.深度缓冲(Z-Buffer)是一个二维数组,其中的每一个元素对应屏幕上的 ...