社区讨论
哈希求调
P1381单词背诵参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mjeaj145
- 此快照首次捕获于
- 2025/12/20 20:44 2 个月前
- 此快照最后确认于
- 2025/12/22 22:15 2 个月前
CPP
#include <iostream>
#include <string>
#include <map>
using namespace std;
const long long p1=998244353,p2=1000000007;
long long n,m;
struct word
{
long long h1,h2;
bool operator < (const word c) const {return c.h1 < h1;}
};
map<word,long long>s;
map<word,bool>f;
word ar[100010];
int sum=0,ans=1000000000;
long long get_hash(string s,long long P)
{
long long ans=0;
for (int i=0;i<s.size();i++)ans=ans*26+(s[i]-'a'),ans%=P;
return ans;
}
int main()
{
cin>>n;
for (int i=1;i<=n;i++)
{
string x;
cin>>x;
s[(word){get_hash(x,p1),get_hash(x,p2)}]=0;
f[(word){get_hash(x,p1),get_hash(x,p2)}]=0;
}
cin>>m;
int l=1,r=1;
for (int i=1;i<=m;i++)
{
string x;
cin>>x;
ar[i]={get_hash(x,p1),get_hash(x,p2)};
f[(word){get_hash(x,p1),get_hash(x,p2)}]=1;
}
for (auto v : f)if (v.second==0)n--;
while(l<=r&&r<=m)
{
word now=ar[r];
s[now]++;
if (s[now]==1)sum++;
while (s[ar[l]]>1)s[ar[l]]--,l++;
if (sum>=n)ans=min(ans,r-l+1);
//cout<<l<<' '<<r<<' '<<ans<<endl;
r++;
}
cout<<n<<endl<<ans;
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...