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. input 与raw_input的区别

    input只能输入数字 raw_input 既可以输入数字也可以输入字符串 >>> input("input you name:") input you name ...

  2. Head First Servlets & JSP 学习笔记 第四章 —— 作为Servlet

    Servlet的任务是得到一个客户的请求,再发回一个响应. 请求: 容器控制着Servlet的一生,它会创建请求和响应对象.为Servlet创建一个新线程或分配一个线程,另外调用Servlet的ser ...

  3. 关于echarts堆叠图标问题 ,某条数数不需要堆叠的处理

    当直接访问的总量不需要堆叠的时候,将stack改为tiled即可,效果图如下

  4. MYSQL分析慢查询

    mysql慢查询的日志文件路径一般为: /var/lib/mysql/slowquery.log,具体的路径可以通过mysql配置文件(/etc/my.cnf)查询,slow_query_log_fi ...

  5. ORACLE分组查询和统计等

    select flow_id,rw from (select t.flow_id ,rownum as rw from apex_030200.wwv_flow_list_templates t)  ...

  6. spring/spirng boot添加fluent日志-aop

    此项目以aop的形式添加fluent 日志 sample介绍 spring-mvc-aop-helloworld 为spring mvc aop condition toolcommontest 为s ...

  7. String 练习

    package com.hanqi; import java.util.Random; public class Text { public static void main(String[] arg ...

  8. spring boot (三): 热部署

    介绍了Spring boot实现热部署的两种方式,这两种方法分别是使用 Spring Loaded和使用spring-boot-devtools进行热部署. 热部署是什么 大家都知道在项目开发过程中, ...

  9. C单链表操作

    #include <stdio.h> #include <stdlib.h> #define ElemType int #define Status int #define O ...

  10. [转载] Linux中的搜索文件命令

    搜索文件用处很大,我们往往需要知道一个文件存放在什么地方,我们又知道Linux是命令强大的一个系统,所以也有好多非常优秀的搜索命令.通常find不常用,因为速度慢,耗费硬盘空间.通常我们先使用wher ...