本文主要是介绍杰西卡考试复习尺取法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
因为课本的内容由int类型整数代替,如果用数组记录,数组需要开到比代表课本内容int整数还要大才可以方便记录,占内存,所以可以用map代替
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
using namespace std;
const int maxn = 1e6 + 5;
int a[maxn];
int main()
{int n;while(~scanf("%d",&n)){set<int> S;for(int i = 1; i <= n; i++){scanf("%d",&a[i]);S.insert(a[i]);}int sum = S.size();int s, e;s = e = 1;int ans = 0;int rec = 0x7fffffff;map<int,int> book;while(1){while(ans < sum && e < n + 1){if(!book[a[e++]]++)ans++;}if(ans < sum) break;rec = min(rec,e-s+1);if(--book[a[s++]] == 0)ans--;}cout << rec << endl;}return 0;
}
这篇关于杰西卡考试复习尺取法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!