HDU 4029 Distinct Sub-matrix(hash + 後綴數組)

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4029

 

題意:找出一個矩陣中有多少不相同的子矩陣

 

思路:考慮用hash將一個字符串變成一個數字,可以枚舉寬度對每一個寬度w下的字符串進行hash,hash[i][j]即表示s[i][j]到s[i][j + w - 1]字符串的哈希值,再然後按照hash[1][0], hash[2][0]...hash[n][0],*,hash[1][1]...這樣的順序在後綴數組中連接起來,不同列間用特殊數值隔開,最後再將這些特殊符的影響用組合計數的方法消除,方法類似於HDU 4416

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#include <string>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

typedef unsigned long long ull;

const int MAXN = 50000;
const int base = 10007;

typedef long long ll;

int t1[MAXN], t2[MAXN], c[MAXN];

bool cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void da(int str[], int sa[], int Rank[], int height[], int n, int m)
{
    n++;
    int i, j, p, *x = t1, *y = t2;
    for (i = 0; i < m; i++)c[i] = 0;
    for (i = 0; i < n; i++)c[x[i] = str[i]]++;
    for (i = 1; i < m; i++)c[i] += c[i - 1];
    for (i = n - 1; i >= 0; i--)sa[--c[x[i]]] = i;
    for (j = 1; j <= n; j <<= 1)
    {
        p = 0;
        for (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++)c[i] = 0;
        for (i = 0; i < n; i++)c[x[y[i]]]++;
        for (i = 1; i < m; i++)c[i] += c[i - 1];
        for (i = n - 1; i >= 0; i--)sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for (i = 1; i < n; i++)
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
        if (p >= n)break;
        m = p;
    }
    int k = 0;
    n--;
    for (i = 0; i <= n; i++)Rank[sa[i]] = i;
    for (i = 0; i < n; i++)
    {
        if (k)k--;
        j = sa[Rank[i] - 1];
        while (str[i + k] == str[j + k])k++;
        height[Rank[i]] = k;
    }
}

int Rank[MAXN], height[MAXN];
int sa[MAXN], l[MAXN], r[MAXN];

char s[200][200];
map <ull, int> mp;
ull Hash[200][200];

int main()
{
    int t;
    cin >> t;
    for (int ca = 1; ca <= t; ca++)
    {
        printf("Case #%d: ", ca);
        int n, m;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
            scanf("%s", s[i]);

        ll ans = 0;
        memset(Hash, 0, sizeof(Hash));
        for (int w = 1; w <= m; w++)
        {
            mp.clear();
            int cnt = 1, tmp = 0;

            for (int i = 0; i < n; i++)
                for (int j = 0; j + w - 1 < m; j++)
                {
                    Hash[i][j] = Hash[i][j] * base + s[i][j + w - 1] - 'A';
                    if (!mp[Hash[i][j]])
                        mp[Hash[i][j]] = cnt++;
                }

            int num = 0;
            for (int j = 0; j + w - 1 < m; j++)
            {
                for (int i = 0; i < n; i++)
                    r[num++] = mp[Hash[i][j]];

                l[tmp++] = n;
                r[num++] = cnt++;
            }
            r[num] = 0;

            da(r, sa, Rank, height, num, cnt + 10);

            ll sum = 0;
            for (int i = 1; i <= num; i++)
                sum += (num - sa[i] - height[i]);

            ll cc = num;
            for (int i = 0; i < tmp; i++)
            {
                cc -= l[i];
                sum -= (l[i] + 1) * cc;
                cc--;
            }

            ans += sum;
        }
        cout << ans << endl;
    }
    return 0;
}




 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章