Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)
https://codeforces.com/contest/1080/problem/E
题意
有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每一列都是一个回文串,问最多有多少个这样的子矩阵
思路
- 首先可以思考如何暴力,枚举四个边界(子矩阵),然后判定一下子矩阵的每一行每一列,这样的复杂度是nmnmn*m
- 很明显需要找一下规律来优化一下复杂度,
- 对于每一行来说,最多只能存在一种字母的数量是奇数,这样一定可以保证这一行是回文串
- 对于每一列来说,因为不能交换顺序,所以必须具有回文性质(关于中心点对称),这就需要要求每一行相同的字母数量应该相同
- 根据第二个规律,因为只需要验证每一行是否相同,所以可以采用字符串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(字符串哈希,马拉车)的更多相关文章
- Codeforces Round #495 (Div. 2) D. Sonya and Matrix
http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- Codeforces Round #524 (Div. 2) Solution
A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...
- 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 ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题
C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #495 (Div. 2) C. Sonya and Robots
http://codeforces.com/contest/1004/problem/C 题意: 在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q.现在这两个机器 ...
随机推荐
- 了解Queue
在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue, 可以对应着 ...
- [剑指Offer]24-反转链表
题目链接 https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&t ...
- [剑指Offer]58-翻转字符串
题目一 翻转单词顺序 题意 输入一个英文句子,翻转句子中的单词的顺序,但单词内自负的顺序不变.标点符号和普通字母一样处理. 例: 输入:"I am a student." 输出:& ...
- 建立SSH的信任关系
1.在Client上root用户执行ssh-keygen命令,生成建立安全信任关系的证书. Client端 # ssh-keygen -t rsa Generating public/private ...
- thinkphp 查找表并返回结果
使用 M('devices')->where($data)-> execute($resql); 返回的只是int值,表示成功与否,并没有返回search 到的 record . $res ...
- Linux下Python2升级Python3
Linux下Python2的升级方法: 一.下载Python3安装包: 1.在线下载 wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2 ...
- echarts柱状图图例不显示的问题
如果想要图例有效果,legend中数据要和series中name的值保持一致,切记切记,这是我曾经遇到的坑,不保持一致是没有效果的
- [z]Linux下压缩与解压
1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz 被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar -z ...
- Windows删除服务方法
- 3F - Lowest Common Multiple Plus
求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行.你可以假设最 ...