Codeforces 149 E. Martian Strings


正反两遍扩展KMP,维护公共长度为L时。出如今最左边和最右边的位置。

。。

然后枚举推断。。。


E. Martian Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up.

Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string s consisting of uppercase Latin letters. The string's length isn.

"Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers abcd, (1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such that a ≤ i ≤ b or c ≤ i ≤ d. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.

Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.

Input

The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105). The second line contains an integer m (1 ≤ m ≤ 100) — the number of beautiful words. Next m lines contain the beautiful words pi, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.

Output

Print the single integer — the number of different beautiful strings the Martian can see this morning.

Sample test(s)
input
ABCBABA
2
BAAB
ABBA
output
1
Note

Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 and c = 4, d = 5 or of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7.




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

using namespace std;

const int INF=0x3f3f3f3f;

const int maxn=101000;

char T[maxn],P[maxn/100];
int next[maxn/10],ex[maxn];

char rT[maxn],rP[maxn/100];
int rnext[maxn/100],rex[maxn];
int LEFT[maxn/100],RIGHT[maxn/100];

int n,q,m;

void pre_exkmp(int next[],char P[],int m)
{
	next[0]=m;
	int j=0,k=1;
	while(j+1<m&&P[j]==P[j+1]) j++;
	next[1]=j;
	for(int i=2;i<m;i++)
	{
		int p=next[k]+k-1;
		int L=next[i-k];
		if(i+L<p+1) next[i]=L;
		else
		{
			j=max(0,p-i+1);
			while(i+j<m&&P[i+j]==P[j]) j++;
			next[i]=j; k=i;
		}
	}
}

void get_limit(int kind,int pos,int ex)
{
	if(kind==0)
	{
		if(LEFT[ex]==INF)
		{
			LEFT[ex]=pos;
		}
	}
	else if(kind==1)
	{
		if(RIGHT[ex]==-1)
		{
			RIGHT[ex]=n-1-pos;
		}
	}
}

void gao(int kind,int m)
{
	if(kind==1)
	{
		int last=-1;
		for(int i=m;i>=1;i--)
		{
			if(RIGHT[i]<last)
			{
				RIGHT[i]=last;
			}
			else
			{
				last=RIGHT[i];
			}
		}
	}
	else
	{
		int last=INF;
		for(int i=m;i>=1;i--)
		{
			if(LEFT[i]>last)
			{
				LEFT[i]=last;
			}
			else
			{
				last=LEFT[i];
			}
		}
	}
}

///kind 0...LEFT  1...RIGHT
void exkmp(int kind,int& mx,int ex[],int next[],char P[],char T[],int n,int m)
{
	pre_exkmp(next,P,m);
	int j=0,k=0;
	while(j<n&&j<m&&P[j]==T[j]) j++;
	ex[0]=j;

	mx=max(mx,ex[0]);
	get_limit(kind,0,ex[0]);

	for(int i=1;i<n;i++)
	{
		int p=ex[k]+k-1;
		int L=next[i-k];	
		if(i+L<p+1) ex[i]=L;
		else
		{
			j=max(0,p-i+1);
			while(i+j<n&&j<m&&T[i+j]==P[j]) j++;
			ex[i]=j; k=i;
		}
		mx=max(mx,ex[i]);
		get_limit(kind,i,ex[i]);
	}
}

int main()
{
	scanf("%s",T);
	n=strlen(T);
	for(int i=0;i<n;i++)
		rT[i]=T[n-1-i];
	int ans=0;
	scanf("%d",&q);
	while(q--)
	{
		scanf("%s",P);
		int m=strlen(P);
		for(int i=0;i<m;i++)
			rP[i]=P[m-1-i];

		memset(LEFT,63,sizeof(LEFT));
		memset(RIGHT,-1,sizeof(RIGHT));

		int mx1=0,mx2=0;
 		exkmp(0,mx1,ex,next,P,T,n,m);
 		gao(0,m);
		exkmp(1,mx2,rex,rnext,rP,rT,n,m);
		gao(1,m);

		if(mx1+mx2<m) continue;

		bool flag=false;

		for(int len=1;len<m&&flag==false;len++)
		{

			if(LEFT[len]==INF||RIGHT[m-len]==-1) continue;
			if(LEFT[len]+len-1<RIGHT[m-len]-(m-len)+1)
			{
				ans++;
				flag=true;
			}
		}
	}
	printf("%d
",ans);	
	return 0;
}


原文地址:https://www.cnblogs.com/zsychanpin/p/7140770.html