POJ 2185 Milking Grid KMP迴圈節周期

來源:互聯網
上載者:User

標籤:blog   http   2014   io   for   re   

題目來源:POJ 2185 Milking Grid

題意:至少要多少大的子矩陣 可以覆蓋全圖

例如範例 可以用一個AB 組成一個

ABABAB

ABABAB 可以多出來

 

思路:每一行求出周期 總共n個 求這n個周期的最小公倍數 如果大於m 取m

           每一列求出周期 總共m個求這個m個周期的最小公倍數 如果大於n取n

答案就是2個最小公倍數的積

#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>using namespace std;const int maxn = 10010;char a[maxn][77];char b[77][maxn]; int f[maxn][77];int f2[77][maxn];int gcd(int a, int b){return b?gcd(b, a%b):a;}void getFail(char* p, int* f){int m = strlen(p);f[0] = f[1] = 0;for(int i = 1; i < m; i++){int j = f[i];while(j && p[i] != p[j])j = f[j];f[i+1] = p[i] == p[j] ? j+1 : 0;}}int main(){int n, m;scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++){scanf("%s", a[i]);getFail(a[i], f[i]);}for(int i = 0; i < m; i++){for(int j = 1; j <= n; j++){b[i+1][j-1] = a[j][i];}b[i+1][n] = 0;}for(int i = 1; i <= m; i++)getFail(b[i], f2[i]);int ans1 = 1, ans2 = 1;for(int i = 1; i <= n; i++){ans1 = ans1/gcd(ans1, m-f[i][m])*(m-f[i][m]);if(ans1 > m){ans1 = m;break;}}for(int i = 1; i <= m; i++){ans2 = ans2/gcd(ans2, n-f2[i][n])*(n-f2[i][n]);if(ans2 > n){ans2 = n;break;}}printf("%d\n", ans1*ans2);return 0;}


 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.