Codeforces Contest 1080 E Sonya and Matrix Beauty —— 马拉车求回文矩形个数

Sonya had a birthday recently. She was presented with the matrix of size n×m and consist of lowercase Latin letters. We assume that the rows are numbered by integers from 1 to n from bottom to top, and the columns are numbered from 1 to m from left to right.

Let’s call a submatrix (i1,j1,i2,j2) (1≤i1≤i2≤n;1≤j1≤j2≤m) elements aij of this matrix, such that i1≤i≤i2 and j1≤j≤j2. Sonya states that a submatrix is beautiful if we can independently reorder the characters in each row (not in column) so that all rows and columns of this submatrix form palidroms.

Let’s recall that a string is called palindrome if it reads the same from left to right and from right to left. For example, strings abacaba,bcaacb,a are palindromes while strings abca,acbba,ab are not.

Help Sonya to find the number of beautiful submatrixes. Submatrixes are different if there is an element that belongs to only one submatrix.

Input
The first line contains two integers n and m (1≤n,m≤250) — the matrix dimensions.

Each of the next n lines contains m lowercase Latin letters.

Output
Print one integer — the number of beautiful submatrixes.

Examples
inputCopy
1 3
aba
outputCopy
4
inputCopy
2 3
aca
aac
outputCopy
11
inputCopy
3 5
accac
aaaba
cccaa
outputCopy
43
Note
In the first example, the following submatrixes are beautiful: ((1,1),(1,1))?(1,2),(1,2)); ((1,3),(1,3))?(1,1),(1,3)).

In the second example, all submatrixes that consist of one element and the following are beautiful: ((1,1),(2,1)); ((1,1),(1,3))?(2,1),(2,3))?(1,1),(2,3))?(2,1),(2,2)).

Some of the beautiful submatrixes are: ((1,1),(1,5))?(1,2),(3,4)); ((1,1),(3,5)).

The submatrix ((1,1),(3,5)) is beautiful since it can be reordered as:

accca
aabaa
accca
In such a matrix every row and every column form palindromes.

题意:

给你一个矩阵,让你选一些子矩阵,你可以重排每一行的数,使得这个矩阵的每一行,每一列都是回文串,那么这就是一个回文矩阵,问你总共有多少个回文矩阵

题解:

完全想不到是马拉车,一直在想hash之后怎么办,怎么处理出有多少回文矩阵,搜了题解发现是马拉车。。那么问题就很好解决了。我们枚举行的所有左右区间,对每种区间都对所有列做一个马拉车,首先可以肯定的是,如果回文,那么这一行就不能有超过一个奇数的数,处理出来之后哈希,然后马拉车就好了,p存的就是回文半径,由于是加上’#'符号的,所以要除二。
时间复杂度O(n^3)
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const int N=255;
char mp[N][N];
ll has[N][N],ans;
ll s[N],ss[2*N];
int p[2*N],n,m,cnt[N][30],odd[N*2];
int judge(int x,int y)
{
    if(odd[x]>1||odd[y]>1)
        return 0;
    return 1;
}
void manacher(int len)
{
    int i,j,len1;
    for(i=0;i<2*len+2;i++)ss[i]=1;
    for(i=0;i<len;i++)ss[i*2+2]=s[i];
    len1=len*2+1;ss[0]=-1;
    int mx=0,id=0;
    //int ans=0;
    for(int i=0;i<len1;i++){
        p[i]=mx>i?min(p[2*id-i],mx-i):1;
        while(ss[i+p[i]]==ss[i-p[i]]&&judge(i+p[i],i-p[i]))p[i]++;
        if(odd[i]>1)
            p[i]=1;
        //if(ans<p[i])ans=p[i];
        if(i+p[i]>mx){
            mx=i+p[i];
            id=i;
        }
        ans+=p[i]/2;
    }
}
void Hash()
{
    for(int i=0;i<n;i++)
    {
        s[i]=0;
        for(int j=0;j<26;j++)
            s[i]=s[i]*37+cnt[i][j]+28;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",mp[i]);
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            odd[j*2+2]=0;
            for(int k=0;k<26;k++)
                cnt[j][k]=0;
        }
        for(int j=i;j<m;j++)
        {
            for(int k=0;k<n;k++)
            {
                cnt[k][mp[k][j]-'a']++;
                if(cnt[k][mp[k][j]-'a']%2)
                    odd[k*2+2]++;
                else
                    odd[k*2+2]--;
            }
            Hash();
            manacher(n);
        }
    }
    printf("%llu\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值