codeforces 149E Z-box / Z-algorithm string matching

The meaning of problems

To give you a text string, the string matching n below, you ask how many of those may be made of p s [i ~ j] + s [l ~ r] from virtue, can degenerate into a paragraph (j = l);

Thinking

Z-box template title, pos array is the soul

Were treated out of p + '#' + s (void getz ()) P + '#' + S (void getZ ()) z-array (uppercase lowercase reverse order),

POS [i] records the length of the minimum right boundary of the front half of the field i, i.e. Getz () in pos [z [i] - len ] = min (pos [z [i] - len], i + z [i] - 1 - len); enumeration taken min: for (int len = I -. 1; I> =. 1; i--) POS [I] = min (POS [I +. 1] -. 1, POS [I] ); // [ah ah ah soul thus treated is out of the boundary of minimal]

Good After pos ok, getZ (), for each bit (i> len) on P, it is determined whether a half after spell --if (pos [len - z [i]] <= len + lEN - i - z [i] + 1); // pos [len - z [i]] is a front half of the right boundary is determined whether the current sub-segment is less than equal to the left margin as the latter half.

Middle wa several reasons for hair should be pos [0] = 0  I always equal to inf and did not update pos [0] to give me the whole silly friends

ACcode

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;
const int MaxN = 1e5 + 1115;
const int inf = 0x3f3f3f3f;

int n,cnt,len,LEN;
char s[MaxN],S[MaxN],p[MaxN],P[MaxN];
int pos[MaxN],z[MaxN];

void getz(){
	int l = 0,r = 0;
	z[0] = 0;
	for(int i = 1;i <= LEN + len; i++){
		if(i > r){
			int n = 0;
			while(p[n] == p[i + n]) n++;
			if(n){
				l = i;
				r = i + n - 1;
			}
			z[i] = n;
		}
		else{
			if(z[i - l] < r - i + 1) z[i] = z[i - l];
			else{
				int n = 1;
				while(p[r - i + n] == p[r + n]) n++;
				r = r + n - 1;
				l = i;
				z[i] = r - l + 1;
			}
		}
		if(i > len) pos[z[i]] = min(pos[z[i]],i + z[i] - 1 - len);
		//pos[i]记录长度为i的字段的最小位置 from 1
	}
	for(int i = len - 1;i >= 1; i--) pos[i] = min(pos[i + 1] - 1,pos[i]);
}

void getZ(){
	int l = 0,r = 0;
	z[0] = 0;
	for(int i = 1;i <= LEN + len; i++){
		if(i > r){
			int n = 0;
			while(P[n] == P[i + n]) n++;
			if(n){
				l = i;
				r = i + n - 1;
			}
			z[i] = n;
		}
		else{
			if(z[i - l] < r - i + 1) z[i] = z[i - l];
			else{
				int n = 1;
				while(P[r - i + n] == P[r + n]) n++;
				r = r + n - 1;
				l = i;
				z[i] = r - i + 1;
			}
		}
		
		if(i > len){//i是倒数第i个
			if(pos[len - z[i]] <= LEN + len - i - z[i] + 1){
				//pos[len - z[i]]:前半段右边界min
				cnt++;//存在多种拆分答案
				break;
			}
		}
	}
}

int main()
{
	scanf("%s",s);
	scanf("%d",&n);
	LEN = strlen(s);
	for(int i = 0;i < LEN; i++) S[i] = s[LEN - i - 1];
	while(n--){
		for(int i = 1;i < MaxN; i++) pos[i] = inf;
		scanf("%s",p);
		len = strlen(p);
		if(len == 1) continue;
		p[len] = '#';
		for(int i = 0;i < LEN; i++) p[len + i + 1] = s[i];
		getz();
		for(int i = 0;i < len; i++) P[i] = p[len - i - 1];
		P[len] = '#';
		for(int i = 0;i < LEN; i++) P[len + i + 1] = S[i];
		getZ();
	}
	printf("%d\n",cnt);
}

 

Published 31 original articles · won praise 5 · Views 1364

Guess you like

Origin blog.csdn.net/qq_43685900/article/details/103163058