| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
后面多余的尾巴可以用next数组去掉分别求行和列的next数组,去掉尾巴,再求最少循环数,行列的最小循环数相乘就是解。
#include <iostream>
#include <cstring>
using namespace std;
char s[10005][80];
int next[10005];
int r,c;
int mr,mc;
bool cmp(int x,int y){
for(int i=0;i<mr;i++){
if(s[i][x]!=s[i][y]) return 0;
}
return 1;
}
int main(int argc, char** argv) {
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++)
scanf("%s",s[i]);
next[0]=0;
for(int i=1;i<r;i++){
int t=next[i-1];
while(t&&strcmp(s[t],s[i]))t=next[t-1];
if(strcmp(s[t],s[i])==0) next[i]=t+1;
else next[i]=0;
}
int x=r-next[r-1]; //去掉尾巴
if(x%(x-next[x-1])==0) mr=x-next[x-1]; //求最少循环数
else mr=x;
next[0]=0;
for(int i=1;i<c;i++){
int t=next[i-1];
while(t&&(!cmp(i,t)))t=next[t-1];
if(cmp(i,t)) next[i]=t+1;
else next[i]=0;
}
x=c-next[c-1];
if(x%(x-next[x-1])==0) mc=x-next[x-1];
else mc=x;
printf("%d\n",mc*mr);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator