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

求数据。。。WA无数次-。-!

Posted by wushangyangguang at 2012-08-13 11:25:06 on Problem 1226 and last updated at 2012-08-13 11:30:44
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#include<math.h>
#define MAX 1500
struct qq
{
	//int id;
	char c[MAX];
	int len;//每个串长
}line[MAX];
int max=0,len,next[MAX];//len是要比较的串长
int maxx(int a,int b)
{
	if(a>b)
		return a;
	return b;
}

int cmp(const void *a,const void *b)
{
	return (*(struct qq*)a).len-(*(struct qq*)b).len;
}
void getnext(char *pat)
{
	int i=0,j=-1;
	memset(next,0,sizeof(next));
	next[0]=-1;
	while(i<len)
	{
		if(j==-1||pat[i]==pat[j])
		{
			i++;
			j++;
			next[i]=j;
		}
		else
			j=next[j];
	}
	return ;
}
int KMP(int o,char *p)
{
	int i=0,j=0;
	while(i<line[o].len&&j<len)
	{
		if(j==-1||line[o].c[i]==p[j])
		{
			i++;
			j++;
		}
		else
			j=next[j];
	}
	if(j==len)
		return max=j;
	else
		return 0;
}
int main()
{
	int i,j,k,n,m,o,u,t,maxl=-1;
	char temp[MAX],tempf[MAX];
	scanf("%d",&n);
	for(k=0;k<n;k++)
	{
		max=-1;
		maxl=-1;
		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			scanf("%s",line[i].c);
			line[i].len=strlen(line[i].c);
		}
		if(m==1)
			printf("%d\n",line[0].len);
		else
		{
			qsort(line,m,sizeof(line[0]),cmp);
			for(i=0;i<line[0].len;i++)
			{

				for(j=i;j<line[0].len;j++)
				{
					memset(temp,0,sizeof(temp));
					memset(tempf,0,sizeof(tempf));
					if(abs(i-j)+1<=maxl)//剪枝长度小于目前最大长度就舍去
						continue;
					if(i<=j)//没什么必要
					{
						u=0;
						for(o=i;o<=j;o++)
						{
							temp[u++]=line[0].c[o];
						}
						len=u;
					}
					/*		else
					{
					u=0;
					for(o=i;o>=j;o--)
					{
					temp[u++]=line[0].c[o];
					}
					len=u;
					}
					*/
					for(t=0;t<len;t++)//反序串
					{
						tempf[len-t-1]=temp[t];
					}
					for(o=1;o<m;o++)
					{
						getnext(temp);
						if(KMP(o,temp))
						{
							continue;
						}
						getnext(tempf);
						if(KMP(o,tempf))
						{
							continue;
						}
						break;
					}
					if(o==m)
						maxl=maxx(max,maxl);
					else
						break;//剪枝当前串不行的话以后包括改串的例子也不行
				}
			}
			printf("%d\n",maxl);
		}
	}
}

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