Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

后面多余的尾巴可以用next数组去掉

Posted by zhjou at 2014-08-05 21:20:47 on Problem 2185
分别求行和列的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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator