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

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

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

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

  1. #include <iostream>
  2. #include <random>
  3. #include <map>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #include <string.h>
  7. #define PER(i,a,n) for(int i=n;i>=a;--i)
  8. #define REP(i,a,n) for(int i=a;i<=n;++i)
  9. #define hr puts("")
  10. using namespace std;
  11. typedef long long ll;
  12. const int N = 1e3+10, P = 998244353;
  13.  
  14. int n, m, rad[N], fac[N];
  15. int a[N], b[N], g[N];
  16. char s[N][N];
  17.  
  18. void manacher(int *a, int n) {
  19. for (int i=1,j=0,k=-1; i<=n; i+=k) {
  20. while (a[i-j-1]==a[i+j+1]) ++j;
  21. rad[i] = j;
  22. for (k=1; k<=rad[i]&&rad[i-k]!=rad[i]-k; ++k) {
  23. rad[i+k] = min(rad[i-k], rad[i]-k);
  24. }
  25. j = max(j-k, 0);
  26. }
  27. }
  28.  
  29. int calc(int *a, int n) {
  30. if (n<=0) return 0;
  31. b[1] = P+1;
  32. REP(i,1,n) {
  33. b[i*2] = a[i];
  34. b[i*2+1] = P+1;
  35. }
  36. int ans = n;
  37. n = 2*n+1, b[n+1] = P+2;
  38. manacher(b,n);
  39. REP(i,1,n) ans += rad[i]/2;
  40. return ans;
  41. }
  42.  
  43. int main() {
  44. fac[0] = 1;
  45. REP(i,1,30) fac[i] = fac[i-1]*991ll%P;
  46. scanf("%d%d", &n, &m);
  47. REP(i,1,n) scanf("%s",s[i]+1);
  48. ll ans = 0;
  49. REP(L,1,m) {
  50. REP(i,0,n) a[i] = g[i] = 0;
  51. REP(R,L,m) {
  52. int now = 0;
  53. REP(i,1,n) {
  54. a[i] = (a[i]+fac[s[i][R]-'a'])%P;
  55. g[i] ^= 1<<s[i][R]-'a';
  56. if (g[i]&(g[i]-1)) {
  57. ans += calc(a+now,i-1-now);
  58. now = i;
  59. }
  60. }
  61. ans += calc(a+now,n-now);
  62. }
  63. }
  64. printf("%lld\n", ans);
  65. }

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. JVM 类加载器ClassLoader源码学习笔记

    类加载 在Java代码中,类型的加载.连接与初始化过程都是在程序运行期间完成的. 类型可以是Class,Interface, 枚举等. Java虚拟机与程序的生命周期 在如下几种情况下,Java虚拟机 ...

  2. postgresQL 服务器端守护进程

  3. PHP如何防止注入及开发安全

    1.PHP注入的基本原理 程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对 用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据 库查询代码,根据程序返回 ...

  4. PHP7 serialize_precision 配置不当导致 json_encode() 浮点小数溢出错误

    https://blog.csdn.net/moliyiran/article/details/81179825 感谢 @地狱星星:原因已找到, 该现象只出现在PHP 7.1+版本上建议使用默认值 s ...

  5. flutter Checkbox 复选框组件

    import 'package:flutter/material.dart'; class CheckboxDemo extends StatefulWidget { @override _Check ...

  6. tr -d命令删除与字符无关的符号

    echo "/192.168"| tr -d '/' 结果:192.168

  7. 使用cmi工具连接服务器远程装机exsi

    使用cmi工具连接服务器远程装机exsi 网宿机房有两台服务器磁盘坏掉了,后面换了磁盘需要重新初始化系统 访问:http://192.168.48.133/cgi/url_redirect.cgi?u ...

  8. 我的一个PLSQL存储过程【我】 改版,加入日志表

    创建日志表sql: -- Create table create table PROCEDURE_LOG ( ID ) not null, NAME ), CODE NUMBER, MSG ), IN ...

  9. Exception in thread "main" brut.androlib.AndrolibException: Could not decode arsc file at brut.androlib.res.decoder.ARSCDecoder.decode

    使用ApkIDE反编译出现如下错误: Exception in thread "main" brut.androlib.AndrolibException: Could not d ...

  10. osg塔吊模拟-20191026

    在osg中模拟塔吊群作业