P1381 单词背诵

题目

题目

思路

第一问hash求解(素数一定要出乎意料,不然毒瘤出题人卡)
第二问用喜闻乐见的尺取法,详见代码
code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int uu[1001],m,o[100001],uo[100001][2],ls,yu[133333],n,ans1,ans2=0x7fffffff;
int book[133333];
string h;
int f(string x)
{
    
    
	int yy=0;
	for (int i=1;i<=x.size();i++) yy=(yy*97+x[i-1])%133331;
	return yy;
}
int main()
{
    
    
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
    
    
		cin>>h;
		uu[i]=f(h);
		book[uu[i]]=1;
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
    
    
		cin>>h;
		o[i]=f(h);
		if (book[o[i]]==1)
		{
    
    
			book[o[i]]=-1;
			ans1++;
		}
	}
	printf("%d\n",ans1);
	if (ans1==0)
	{
    
    
		printf("0");
		return 0;
	}//由于代码对ans1进行判断是否=0的操作,所以我们特判0,0情况
	int l=1;
	int r=1;
	while (20!=0)
	{
    
    
		if (ans1==0)
		{
    
    
			while (book[o[l]]==0) l++;
			if (l>m) break;
			ans2=min(r-l,ans2);
			if (yu[o[l]]==1) ans1++;
			yu[o[l]]--,l++;
		}
		else
		{
    
    
			if (r>m) break;
			if (book[o[r]])
			{
    
    
				if (yu[o[r]]==0) ans1--;
				yu[o[r]]++;
			}
			r++;
		}
	}
	printf("%d",ans2);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/115210638
今日推荐