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

KMP172k0ms!自己写的模板大家可以拿去(擠进2000名纪念)

Posted by KatrineYang at 2016-08-29 10:23:24 on Problem 1226 and last updated at 2016-08-29 10:24:26
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;


void buildBorderArray(int *border, char* s, int size, int dir) {
     //int size = strlen(s);
     //cout << s.size() << endl;
     //border = new int[s.size() + 1];
     border[0] = -1;
     for(int i = 0; i < size-1; i++){
             int bor = border[i];
             while(bor >= 0 && *(s+(i+1)*dir) != *(s+ (bor + 1)*dir)){
                       bor = border[bor];
             }
             if(*(s+(i+1)*dir) == *(s+(bor+1)*dir)) border[i+1] = bor + 1;
             else border[i+1] = -1;
             //cout << border[i+1] << endl;
     }
}

int searchKmp(char* str, int strsize, int strdir, char* pat, int patsize, int patdir, int *border){
	//看能匹配多长,返回长度
            //vector<int> res;
            //int patsize = strlen(pat);
            //if(strsize < patsize) return -1;
            //int border[1005];
           // buildBorderArray(border, pat, patsize);
            int i = 0, j = 0, mx = 0;
            while(i <= strsize){
                    while(j < patsize && i < strsize && *(str+i*strdir) == *(pat+j*patdir)){
                            i++; j++;
                            //cout << i << " " << j << endl;
                    }
                    if(j == patsize) {
                    	return patsize;
                    }
                    if(mx < j) mx = j;
                    if(j == 0) i++;
                    else j = border[j-1] + 1;
                    if(mx < j) mx = j;
            }
            return mx;
}

int main() {
	int T;
	scanf("%d", &T);
	for(int ii = 0; ii < T; ii++){
		char str[110][110];
		int N;
		scanf("%d", &N);
		for(int i = 0; i < N; i++){
			scanf("%s", str[i]);
		}
		if(N == 1){
			printf("%d\n", strlen(str[0]));
			continue;
		}
		int len[110], minLen = 110, minArg = -1;
		for(int i = 0; i < N; i++){
			int tempLen = strlen(str[i]);
			len[i] = tempLen;
			if(tempLen < minLen) {
				minLen = tempLen;
				minArg = i;
			}
		}
		//cout << minLen << " " << minArg << endl;
		int yjpp = 0;
		for(int chang = minLen; chang >= 1; chang--){
			if(chang <= yjpp) break;
			int border[110];
			buildBorderArray(border, str[minArg] + minLen - chang, chang, 1);
			int zdpp = chang;
			for(int i = 0; i < N; i++){
				if(i == minArg) continue;
				int zxpp = searchKmp(str[i], len[i], 1, str[minArg] + minLen - chang, chang, 1, border);
				int fxpp = searchKmp(str[i]+len[i]-1, len[i], -1, str[minArg] + minLen - chang, chang, 1, border);
				//cout << zxpp << " " << fxpp << endl;
				int temp = (zxpp > fxpp)? zxpp : fxpp;
				if(temp < zdpp) zdpp = temp;
			}
			if(zdpp > yjpp) yjpp = zdpp;
			if(chang <= yjpp) break;
			zdpp = chang;
			buildBorderArray(border, str[minArg] + chang - 1, chang, -1);
			for(int i = 0; i < N; i++){
				if(i == minArg) continue;
				int zxpp = searchKmp(str[i], len[i], 1, str[minArg] + chang - 1, chang, -1, border);
				int fxpp = searchKmp(str[i]+len[i]-1, len[i], -1, str[minArg] + chang - 1, chang, -1, border);
				//cout << zxpp << " " << fxpp << endl;
				int temp = (zxpp > fxpp) ? zxpp : fxpp;
				if(temp < zdpp) zdpp = temp;
			}
			if(zdpp > yjpp) yjpp = zdpp;
			//cout << endl;
		}
		printf("%d\n", yjpp);
	}
	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