大意: 给定$nm$字符串矩阵. 若一个子矩形每一行重排后可以满足每行每列都是回文, 那么它为好矩形. 求所有好矩形个数.

一个矩形合法等价于每一行出现次数为奇数的最多只有一个字符, 并且对称的两行对应字符出现次数要完全相等.

那么直接暴力枚举左右边界, 把每个字符的出现次数$hash$一下, 这样就转化为给定序列, 求回文子串个数. 这是manacher算法经典应用, 套板子即可.

暴力计算次数的话$O(26n^3)$竟然没卡过去, 改了好久最后位运算优化到$O(n^3)$才过.

#include <iostream>
#include <random>
#include <map>
#include <cstdio>
#include <algorithm>
#include <string.h>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define hr puts("")
using namespace std;
typedef long long ll;
const int N = 1e3+10, P = 998244353; int n, m, rad[N], fac[N];
int a[N], b[N], g[N];
char s[N][N]; void manacher(int *a, int n) {
for (int i=1,j=0,k=-1; i<=n; i+=k) {
while (a[i-j-1]==a[i+j+1]) ++j;
rad[i] = j;
for (k=1; k<=rad[i]&&rad[i-k]!=rad[i]-k; ++k) {
rad[i+k] = min(rad[i-k], rad[i]-k);
}
j = max(j-k, 0);
}
} int calc(int *a, int n) {
if (n<=0) return 0;
b[1] = P+1;
REP(i,1,n) {
b[i*2] = a[i];
b[i*2+1] = P+1;
}
int ans = n;
n = 2*n+1, b[n+1] = P+2;
manacher(b,n);
REP(i,1,n) ans += rad[i]/2;
return ans;
} int main() {
fac[0] = 1;
REP(i,1,30) fac[i] = fac[i-1]*991ll%P;
scanf("%d%d", &n, &m);
REP(i,1,n) scanf("%s",s[i]+1);
ll ans = 0;
REP(L,1,m) {
REP(i,0,n) a[i] = g[i] = 0;
REP(R,L,m) {
int now = 0;
REP(i,1,n) {
a[i] = (a[i]+fac[s[i][R]-'a'])%P;
g[i] ^= 1<<s[i][R]-'a';
if (g[i]&(g[i]-1)) {
ans += calc(a+now,i-1-now);
now = i;
}
}
ans += calc(a+now,n-now);
}
}
printf("%lld\n", ans);
}

Sonya and Matrix Beauty CodeForces - 1080E (manacher)的更多相关文章

  1. Sonya and Matrix Beauty Codeforces - 1080E

    https://codeforces.com/contest/1080/problem/E 比赛时候一个多小时码不出来... 来看遇到的困难: 1.没有能用的随机unsignedlonglong函数 ...

  2. 【题解】Sonya and Matrix Beauty [Codeforces1080E]

    [题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...

  3. Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)

    https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...

  4. codeforces 495D Sonya and Matrix

    Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Son ...

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

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

  6. Sonya and Matrix CodeForces - 1004D (数学,构造)

    http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...

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

    正常没有正方形的限制下,值为i的点个数4i 那么从0开始遍历,第一个不为4i的值就是min(x, y) 由于对称性我们姑且令x为这个值 我们先列举n*m=t的各种情况 对于一对n, m.我们已经知道n ...

  8. Sonya and Robots(CodeForces 1004C)

    Since Sonya is interested in robotics too, she decided to construct robots that will read and recogn ...

  9. D. Vasya And The Matrix(Educational Codeforces Round 48)

    D. Vasya And The Matrix time limit per test2 seconds memory limit per test256 megabytes inputstandar ...

随机推荐

  1. Linux_CentOS常用命令和shell命令技巧

    Linux_CentOS常用命令 关机 init 重启 init 列出当前目录的下的文件 ls //列出当前目录下的文件 ll //列出当前目录下的文件信息 等同ls -l 命令 切换目录 cd 目录 ...

  2. Spring中好玩的注解和接口

    测试中: 一.unit中集中基本注解,是必须掌握的. @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意p ...

  3. 代理类和AOP

    客户端不用调用目标对象了,直接调用代理类.最终目标方法还是去实行了. 代理类的每个方法调用目标类的相同方法,并且在调用方法时候加上系统功能的代码 代理和目标实现了相同的接口,有相同的方法.通过接口进行 ...

  4. matlab学习笔记4--MAT文件的保存和读取

    一起来学matlab-matlab学习笔记4 数据导入和导出_1 MAT文件的保存和读取 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用&g ...

  5. 转 oracle healthcheck

    ##sample 0 https://carlos-sierra.net/2013/11/01/a-healthy-way-to-do-an-oracle-database-health-check/ ...

  6. gen语言

    概率编程语言(PPL)领域正经历着机器学习技术快速发展带来的奇迹般的复兴.在短短的几年里,PPL 已经从一个模糊的统计研究领域发展出十几个活跃的开源方案.最近,麻省理工学院(MIT)的研究人员推出了一 ...

  7. 使用mysqlslap测试MySQL性能

    之前介绍过MySQL的性能测试工具sysbench, 这次介绍一个mysql自带的比较简单的性能测试命令mysqlslap. 下面的指令模拟了1000个进程同事连接mysql,并执行100个查询操作, ...

  8. curl --resolve 查看证书情况

    通过curl  解析证书 [root@harbor ~]# curl --resolve 'www.abc.com:127.0.0.1' https://www.abc.com/ -vvv * Cou ...

  9. Error: python-devel conflicts with python-2.7.5-68.el7.x86_64

    yum install yum-utils -y package-cleanup --cleandupes yum -y install python-devel yum -y install pyt ...

  10. PHP提高in_array查找元素的方法

    PHP提高in_array查找元素的方法<pre><?php$arr = array(); // 创建10万个元素的数组for($i=0; $i<100000; $i++){ ...