[2019] Multi-school Joint Examination No.2-journey

Preface

Similarly, see "energy"

subject

Sample Input 1

aaaa

aaaa

Sample Output  1

4

Sample Input  2

a*a

aaaaaa

Sample Output  2

6

Sample Input  3

*a*b*c*

abacabadabacaba

Sample Output  3

15

[Data Scope]

For 30% of the data, M<=20;

For 80% of the test points, M<=200;

For 100% test points, 1<=N<=100,1<=M<=100,000.

Analysis

Too lightly. I played a code, passed the example, thought that 80 points had no control, actually only 9 points, I, I, I, i...

Examination ideas:

Character "*" can be anything, so don't worry, as long as the remaining characters in a appear in b.

So enumerate all the "cyclic isomorphic strings" of b to check whether all the characters in a appear in sequence in b.

It feels like there is nothing wrong with qwq... I don't know what's wrong, and I need to continue to discuss it.

Strange code with only 9 points in the exam

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5;
char a[MAXN+5],b[MAXN+5],tmp[MAXN+5];
int n,m,ans,len;
bool Check(int id)
{
	int cnt=1;
	for(int i=id;i<=id+m-1;i++)
	{
		if(b[i]==tmp[cnt])
			cnt++;
	}
	if(cnt==len+1)
		return true;
	return false;
}
int main()
{
	//freopen("journey.in","r",stdin);
	//freopen("journey.out","w",stdout);
	scanf("%s%s",a+1,b+1);
	n=strlen(a+1);
	m=strlen(b+1);
	for(int i=1;i<=m;i++)
		b[i+m]=b[i];
	for(int i=1;i<=n;i++)
		if(a[i]!='*')
			tmp[++len]=a[i];
	for(int i=1;i<=m;i++)//String opening letter position i 
		if(Check(i))
			ans++;
	printf("%d",ans);
	return 0;
}
/*
Guarantee at least 80% of the break-ups (M<=200) 
'*'Can match any number of characters, [including 0] 
*/

AC code

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1e5;
ll hash[MAXN+5],h[MAXN+5],len[MAXN+5],g[MAXN+5];
int f[2*MAXN+5][100];
char a[MAXN+5],b[2*MAXN+5];
ll n,m,cnt,c,ans;
int main()
{
	//freopen("journey.in","r",stdin);
	//freopen("journey.out","w",stdout);
	scanf("%s%s",a+1,b+1);
	n=strlen(a+1);
	m=strlen(b+1);
	hash[0]=1;
	for(int i=1;i<=n;i++)
		hash[i]=hash[i-1]*13331;
	int tmp=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]=='*')
		{
			if(i>1&&a[i-1]!='*')
			{
				h[++cnt]=tmp;//hash value for each segment 
				len[cnt]=c;//Length of each segment 
				tmp=c=0;
			}
		}
		else
			tmp+=(a[i]-'a'+1)*hash[c++];
	}
	if(a[n]!='*')
		h[++cnt]=tmp,len[cnt]=c;
	for(int i=1;i<=m;i++)
		b[i+m]=b[i];
	if(!cnt)//They are all * *.
	{
		printf("%lld",m);
		return 0;
	} 
	for(int i=1;i<=cnt;i++)//Preprocessing 
		f[m*2+1][i]=f[m*2+2][i]=m*2+1;
	for(int i=m*2;i;i--)//Processing hash value of b string 
	{
		memset(g,-1,sizeof(g));
		g[0]=0;
		for(int j=0;j<n;j++)
			if(i+j<=2*m)//It's a substring 
				g[j+1]=g[j]+(b[i+j]-'a'+1)*hash[j];
		for(int j=1;j<=cnt;j++)
		{
			f[i][j]=f[i+1][j];
			if(g[len[j]]==h[j])//a,b have the same substring
				f[i][j]=i+len[j]-1; 
		}
	}
	int j,k,w;
	for(int i=1;i<=m;i++)
	{
		bool flag=true;
		for(j=n;j&&a[j]!='*';j--)
			if(a[j]!=b[i+m-1-(n-j)])//Find a legal starting position in b 
			{
				flag=false;
				break;
			}
		if(!flag||j==0&&n!=m)
			continue;
		for(k=1,w=i;k<=cnt-(a[n]!='*');k++)//k: Number of segments, w: position 
		{
			if(k==1&&a[1]!='*'&&f[i][k]+1!=i+len[1])
				break;
			w=f[w][k]+1;
		}
		if(k>cnt-(a[n]!='*')&&w<=i+m-(n-j))
			ans++;
	}
	printf("%lld",ans);
	return 0;
}

 

Posted by nova on Sat, 05 Oct 2019 03:38:28 -0700