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. PAT L3-010 是否完全二叉搜索树(二叉搜索树)

    将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数 ...

  2. Django的rest_framework的序列化组件之serializers.ModelSerializer介绍

    这里的介绍的serializers.ModelSerializer就和我们之前学习的modelform一样 serializers.ModelSerializer如下几个功能 1.序列化queryse ...

  3. Xcode 去掉控制台无用打印信息

    1. 2.在Environment Variables增加一键值对 OS_ACTIVITY_MODE = disable 转自:https://blog.csdn.net/HelloWorld_198 ...

  4. git--(3)分支 合并

    git branch test //新建分支 git branch //列出分支 git branch -r //列出远程分支 git branch -m | -M oldbranch newbran ...

  5. /usr/local/ 和 /opt

    /usr/local:用户级的程序目录,可以理解为C:/Progrem Files/.用户自己编译的软件默认会安装到这个目录下. /opt:用户级的程序目录,可以理解为D:/Software,opt有 ...

  6. go语言中结构struct

    package main; import "fmt" //结构struct //定义Person结构 type Person struct { name string; age i ...

  7. Non-negative Integers without Consecutive Ones

    n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecut ...

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

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

  9. JS中判断某个字符串是否包含另一个字符串的五种方法

    String对象的方法 方法一: indexOf()   (推荐) ? 1 2 var str = "123" console.log(str.indexOf("2&qu ...

  10. Linux_(3)Shell编程(上)

    一.shell 简介Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个 ...