Cows matrix

Cows matrix

Gives a \ (R \ times C \) of the character matrix, the minimum coverage interrogation matrix defining a matrix covering for continuous self-replication after expansion to its original character matrix sub-matrix, \ (1≤R≤10000, 1≤C≤75 \) .

solution

From research is simple, do not study the beginning of the two-dimensional, for his party, we find a conclusion, that is, its coverage matrix, bound to be aligned left, draw a diagram of self-understanding, so can be extended to two dimensions, must be align the top left corner.

Also from the point of view of the answer, found the ranks of the independent nature, which is a sub-matrix for unlimited expansion, covering the original matrix, we can press the R-line characters in a row, then it becomes a string of sub-matrix problem, the same process can be carried out on the column, the product of two sub-matrices of such length that we find out the answer (on vague feeling is right, it makes sense, I can not tell).

How will multiple lines pressed into a line, hash it, so the next string becomes a problem, how to find a string of "covering the substring" We need to find algorithms to sit carrier, we learned hash string , kmp, trie, minimal representation, there are some models (e.g., the longest substring palindromic), consider KMP, and then found that the nature of a minimum "covers the substring" is the length of the string length -next [string length ], self-drawing understand what you can.

The last time complexity we can do \ (O (RC) \) .

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define ll long long
#define ull unsigned ll
#define jzm 19260817
using namespace std;
char s[15000][100];
int Next[15000];
ull qz[15000],base[15000];
il int kmp(int);
il void get(char&);
int main(){
    int r,c,ans(1);base[0]=1;
    for(int i(1);i<=10000;++i)
        base[i]=base[i-1]*jzm;
    scanf("%d%d",&r,&c);
    for(int i(1),j;i<=r;++i)
        for(j=1;j<=c;++j)
            get(s[i][j]);
    for(int i(1),j;i<=r;++i)
        for(j=1;j<=c;++j)
            qz[i]=qz[i]*jzm+s[i][j];
    ans=kmp(r),memset(qz,0,sizeof(qz));
    for(int i(1),j;i<=c;++i)
        for(j=1;j<=r;++j)
            qz[i]=qz[i]*jzm+s[j][i];
    ans*=kmp(c),printf("%d",ans);
    return 0;
}
il int kmp(int n){
    for(int i(2),j(0);i<=n;++i){
        while(j&&qz[i]!=qz[j+1])j=Next[j];
        if(qz[i]==qz[j+1])++j;Next[i]=j;
    }return n-Next[n];
}
il void get(char &c){
    while(c=getchar(),c==' '||c=='\n'||c=='\r');
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11257706.html