题目:

解法:定义左索引和右索引

  1.先让右索引往右移,直到得到所有知识点为止;

  2.然后让左索引向右移,直到刚刚能够得到所有知识点;

  3.用右索引减去左索引更新答案,因为这是满足要求的子串。

  4.不断重复1,2,3。直到搜索到最后,不论怎样都获得不了所有的知识点时跳出。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
#define INF 2147483647 int n;
int a[]; map <int,int> m; int main(){
int n;
cin >> n;
set <int> q;
for(int i = ;i < n; i++){
scanf("%d",&a[i]);
q.insert(a[i]);
// cout << a[i] << endl;
}
int num = q.size();
// cout << num << endl;
int res = INF; int sum = ;
int s = ;int t = ;
while(true){
while(t < n && sum != num){
if(m[a[t]] == )
sum++;
m[a[t]]++;
t++;
}
while(m[a[s]] > ){
m[a[s]]--;
s++;
if(m[a[s-]] == ){
sum--;
break;
}
}
if(sum < num-) break;
// cout << s << " " << t << endl;
res = min(res,t-s+);
} cout << res << endl;
return ;
}

最新文章

  1. spring HttpInvoker相关学习资料
  2. 移动Web开发调研
  3. TPS40305 ——开关电源芯片20160901
  4. WebService 生成类的命令语句
  5. aws ftp
  6. MVC4/5+jquery+bootstrap样式+dataTables+linq+WCF+EF6后台和前台的框架集合!好蛋疼哦!数据库支持MYSQL 和MSSQL,oracle。集成腾讯企业邮箱收邮件同步用户SSO登陆等功能。
  7. iPhone开发教程之retain/copy/assign/setter/getter
  8. Android_Gallery
  9. PHP框架原理
  10. hdu 4739
  11. poj 1157 LITTLE SHOP_简单dp
  12. 转:聚类、K-Means、例子、细节
  13. HTML中padding和margin的区别和用法
  14. xml序列化和反序列化(二)
  15. poj2594 机器人寻找宝藏(最小路径覆盖)
  16. socat管理haproxy以及haproxy调优
  17. mac的safari浏览器调试ios手机网页
  18. js怎么把一个数组里面的值作为一个属性添加到另一数组包含的对象里(小程序)
  19. POJ3045--Cow Acrobats(theory proving)
  20. Codeforces 847E - Packmen

热门文章

  1. Redis学习笔记(七) 基本命令:Set操作
  2. C#中网络通信
  3. redis的key对应mysql数据表设计
  4. Vue中问题总结 与未解决问题总结
  5. 软件测试作业-selenium
  6. js 数据类型判断
  7. swfupload组件上传文件
  8. 防范CSRF(二)
  9. 算法22-----托普利茨矩阵leetcode766
  10. Vue的数据依赖实现原理简析