双指针算法求助,已过样例
  • 板块P1381 单词背诵
  • 楼主xC41
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/9/23 22:54
  • 上次更新2024/9/24 05:55:08
查看原帖
双指针算法求助,已过样例
316258
xC41楼主2024/9/23 22:54

RT 用了map

#include <iostream>
#include <map>
#include <cstring>
using namespace std;

int n, m, ans1, ans2 = 0x3f3f3f3f;
string s;
string sr[100005];
map<string, int> word;
map<string, bool> word1;
map<string, int> num;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> s;
		word[s]++;
	}
	cin >> m;
	//#1
	for(int i = 1; i <= m; i++){
		cin >> sr[i];
		if(word[sr[i]] >= 1 && word1[sr[i]] == false){
			ans1++;
			word1[sr[i]] = true;
		}
	}
	cout << ans1 << endl;
	//#2
	int l = 0, r = 0;
	int cnt = 0;
	while(l <= m && r <= m){
		if(cnt < ans1){
			r++;
			num[sr[r]]++;
			if(num[sr[r]] == 1) cnt++; 
		}
		if(cnt == ans1){
			ans2 = min(r - l + 1, ans2);
			l++;
			num[sr[l - 1]]--;
			if(num[sr[l - 1]] == 0) cnt--;
		}
	}
	cout << ans2 << endl;
	return 0;
}
2024/9/23 22:54
加载中...