基本双指针90pts求救
查看原帖
基本双指针90pts求救
502758
ForMyDream楼主2023/8/26 20:23

WA on test 7

#include<iostream>
#include<unordered_map>
using namespace std;
unordered_map <string,int> word,s_has;
int n,m,l,r,len=0x3f3f3f3f,num,cnt[100001],ans,i=1,j=0;
string s[1001],t[100001];
int main(){
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>s[i]; s_has[s[i]]=1;
	}
	cin>>m;
	for (int i=1;i<=m;i++){
		cin>>t[i];
		if (s_has[t[i]]==1) ans++,s_has[t[i]]=2; 
		// t 中有 s 对应单词,则包含要背的数量 +1 
		// s_has 的值为 2 的时候,代表这个单次需要背,且已经算过了 
	}
	if (ans==0){ // 特判,如果要背诵的单次在文中均没有出现 
		cout<<0<<' '<<0;
		return 0;
	}
	while (j<=m){
		if (num<ans){
			j++;
			if (!word[t[j]] && s_has[t[j]]) num++; 
			word[t[j]]++;
		}
		if (num==ans) len=min(len,j-i+1);
		if (num>=ans){
			word[t[i]]--;
			if (!word[t[i]] && s_has[t[i]]) num--;
			i++;
		}
//		cout<<i<<' '<<j<<' '<<num<<endl;
	}
	cout<<ans<<endl<<len;
	return 0;
}

感谢

2023/8/26 20:23
加载中...