liberOJ #6173. Samjia 和矩阵 hash+后缀数组

#6173. Samjia 和矩阵

题目链接  : 点这里

题目描述

给你一个只包含大写字母的矩阵,求有多少本质不同的子矩阵。

输入格式

第一行包含两个整数 nnn , mmm ,表示矩阵 nnn 行 mmm 列 。
接下来 nnn 行描述这个矩阵。

输出格式

只含一个整数,为本质不同的子矩阵个数。

样例

样例输入

3 3
ABA
BAA
AAA

样例输出

22

数据范围与提示

对于 10% 10\%10% 的数据,n,m≤10 n,m \leq 10n,m≤10;
对于 40% 40\%40% 的数据,n,m≤25 n,m \leq 25n,m≤25;
对于 70% 70\%70% 的数据,n,m≤60 n,m \leq 60n,m≤60;
对于 100% 100\%100% 的数据,n,m≤110 n,m \leq 110n,m≤110。

题解:

  

  

来自wannafly

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 2e4+10, M = 1e3+20,inf = 2e9;

int *ran,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
bool cmp(int *r,int a,int b,int l) {
    return r[a] == r[b] && r[a+l] == r[b+l];
}
void SA(int *r,int *sa,int n,int m) {
    int *x=wa,*y=wb,*t;
    for(int i=0;i<m;++i)wm[i]=0;
    for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
    for(int i=1;i<m;++i)wm[i]+=wm[i-1];
    for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
    for(int i=0,j=1,p=0;p<n;j=j*2,m=p){
        for(p=0,i=n-j;i<n;++i)y[p++]=i;
        for(i=0;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
        for(i=0;i<m;++i)wm[i]=0;
        for(i=0;i<n;++i)wm[x[y[i]]]++;
        for(i=1;i<m;++i)wm[i]+=wm[i-1];
        for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
        for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i) {
            x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
        }
    }
    ran=x;
}
void Height(int *r,int *sa,int n) {
    for(int i=0,j=0,k=0;i<n;height[ran[i++]]=k)
    for(k?--k:0,j=sa[ran[i]-1];r[i+k] == r[j+k];++k);
}

const ULL mod = 10000019ULL;
int n,m;
ULL sqr[300],has[120][120];
char a[210][120];
map<ULL,int >mp;
int main() {
    sqr[0] = 1;
    for(int i = 1; i <= 200; ++i) sqr[i] = sqr[i-1] * mod;
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; ++i) {
        scanf("%s",a[i]+1);
        has[i][0] = 0;
        for(int j = 1; j <= m; ++j) {
            has[i][j] = has[i][j-1] * mod + a[i][j] - ‘A‘ + 1;
        }
    }
    LL ans = 0;
    for(int y = 1; y <= m; ++y) {
        int cnt = 0,san = 1;
        mp.clear();
        for(int j = 1; j + y - 1 <= m; ++j) {
             for(int i = 1; i <= n; ++i){
                int l = j, rr = j + y - 1;
                ULL now = has[i][rr] - has[i][l-1]*sqr[y];
                if(mp[now] == 0) mp[now] = san++;
                r[cnt++] = mp[now];
            }
            r[cnt++] = san++;
        }
        r[cnt] = 0;

        SA(r,sa,cnt+1,san);
        Height(r,sa,cnt);
        //for(int i = 0; i <= cnt; ++i) cout<<sa[i]<<" "<<ran[i]<<" "<<height[i]<<endl;
       // return 0;
        ans += n*(n+1)/2*(m-y+1);

        for(int i = 1; i <= cnt; ++i) {
            ans -= height[i];
        }
    }
    printf("%lld\n",ans);
    return 0;
}
时间: 2024-08-26 22:50:12

liberOJ #6173. Samjia 和矩阵 hash+后缀数组的相关文章

后缀数组 hash求LCP BZOJ 4310: 跳蚤

后缀数组的题博客里没放进去过..所以挖了一题写写 充实下博客 顺便留作板子.. 一个字符串S中 内容不同的子串 有 sigma{n-sa[i]+1-h[i]}   (噢 这里的h[]就是大家熟知的height[]) 所以l=1,r=上述sigma 二分 答案是字典序第几大的子串. 然后 求S中第k大的子串W : 因为h[i]是与i-1有关的 所以要从n downto 1,k-=n-sa[i]+1-h[i] 至 k再减就非正了 显然这样扫过来 子串字典序是递减的  因此可以得到第k大子串W 然后再

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符.然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio> #include<algorithm> #include<iostream> #include<cmath> #include<queue> #include<stack> #include<string> #include<cstrin

cf244D. Match &amp; Catch 字符串hash (模板)或 后缀数组。。。

D. Match & Catch 可以用各种方法做,字符串hash,后缀数组,dp,拓展kmp,字典树... 字符串hash(模板) http://blog.csdn.net/gdujian0119/article/details/6777239 BKDR Hash Function : // BKDR Hash Function unsigned int BKDRHash(char *str) { unsigned int seed = 131; // 31 131 1313 13131 13

poj 2774 最长公共子串--字符串hash或者后缀数组或者后缀自动机

http://poj.org/problem?id=2774 想用后缀数组的看这里:http://blog.csdn.net/u011026968/article/details/22801015 本文主要讲下怎么hash去找 开始的时候写的是O(n^2 logn)算法 果断超时...虽然也用了二分的,, 代码如下: //hash+二分 #include <cstdio> #include <cstring> #include <algorithm> #include

poj 2774 最长公共子--弦hash或后缀数组或后缀自己主动机

http://poj.org/problem?id=2774 我想看看这里的后缀数组:http://blog.csdn.net/u011026968/article/details/22801015 本文主要讲下怎么hash去找 開始的时候写的是O(n^2 logn)算法 果断超时. ..尽管也用了二分的.. 代码例如以下: //hash+二分 #include <cstdio> #include <cstring> #include <algorithm> #incl

cf244D. Match &amp;amp; Catch 字符串hash (模板)或 后缀数组。。。

D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian0119/article/details/6777239 BKDR Hash Function : // BKDR Hash Function unsigned int BKDRHash(char *str) { unsigned int seed = 131; // 31 131 1313 13131 1

acdream1116 Gao the string!(hash二分 or 后缀数组)

问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说,所有与它匹配的前缀必然是和 s[i+1...n-1]  s[i+2...n-1] ....s[n-1..n-1]里的前缀匹配的,因而如果我们定义一个num[i]表示的是后缀s[i...n-1]与前缀的总长公共前缀,那么num[i]+num[i+1]+..num[n-1]就是前缀在后缀i里出现的次数

bnuoj 34990(后缀数组 或 hash+二分)

后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀. 我自己在想hash的时候一直在考虑hash成数值时MOD取多大,如果取10^18的话,那么两数相乘个就超LL了,但是取10^9的话又怕出现重复的可能大.后面才发现自己是sb,如果用unsigned long long 如果有溢出或者为负数是直接变成对(1<<64)取模了. 也就是无符号长整形运算自动帮你取模了.所以可以放心用hash Justice S

[bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)

以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列SA[1],SA[2],……,SA[n],并且保证Suffix(SA[i])<Suffix(SA[i+1]),1≤i<n. 也就是将S的n个后缀从小到大进行排序之后把排好序的后缀的开头位置顺次放入SA中. height数组:定义height[i]=suffix(sa[i-1])和suffix(sa[