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

麻痹比,必须把最小的一个串放在第0个数组里面

Posted by liyuweihuo at 2016-03-30 21:10:39 on Problem 1226
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
#pragma comment(linker,"/STACK:102400000,102400000")
#define lowbit(i) (i) & (-i) 
#define LL long long
#define sqr(a) ((a)*(a))
#define MOD 1000000007
#define lson(step) step<<1
#define rson(step) step<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std;
const int INF=0x3f3f3f3f;
const int M=1000+5;
char s[M][M],word[M];;
int Next[M];
void getnext(char ss[])
{
	int i=0,j=-1,l=strlen(ss);
	Next[0]=-1;
	while(i<l)
	{
		if(j==-1||ss[i]==ss[j])
		i++,j++,Next[i]=j;
		else
		j=Next[j];
	}
}
int KMP(char str1[],char str2[])
{
	int i=0,j=0,l1=strlen(str1),l2=strlen(str2);
	if(l1>l2)
	return 0;
	while(i<l2)
	{
		if(j==-1||str1[j]==str2[i])
		i++,j++;
		else
		j=Next[j];
		if(j==l1)
		return 1;
	}
	return 0;
}
int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		memset(s,0,sizeof(s));
		scanf("%s",s[0]);
        char ppp[M],I=0,LLL=strlen(s[0]);
        strcpy(ppp,s[0]);
		for(int i=1;i<n;i++)
		{
		scanf("%s",s[i]);
		if(LLL>strlen(s[i]))
		{
			strcpy(ppp,s[i]);
			LLL=strlen(s[i]);
			I=i;
		}
	    }
	    strcpy(s[I],s[0]);
	    strcpy(s[0],ppp);
	    int l=strlen(s[0]);
	    int num=0;
	    for(int i=1;i<=l;i++)
	    {
	    	int Mark=0;
	    	for(int j=0;i+j<=l;j++)
	    	{
				strncpy(word,s[0]+j,i);
				word[i]='\0';
				char qw[M];
				int L=strlen(word);
				for(int tt=0,t=L-1;t>=0;t--)
				qw[tt++]=word[t];
				qw[L]='\0';
				int mark=0;
				memset(Next,0,sizeof(Next));
				getnext(word);
				for(int t=1;t<n;t++)
				if(!KMP(word,s[t])&&!KMP(qw,s[t]))
				{
					mark=1;
					break;
				} 
				if(mark==0)
				{
				num=i;
				Mark=1;
				break;	
			    }
			}
			if(!Mark)
			break;
		}
		printf("%d\n",num);
	}
	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