uva 11572

来源:互联网 发布:淘宝店铺优化排名 编辑:程序博客网 时间:2024/05/16 12:54

题目大意

给你一串数字

求其中没有重复数字的最长字串


因为数据量大

用map

先用一个数组保存数据

一层for

判断num[i]之前有没有出现过

用map[num[i]]非常方便

如果没出现过

就在map中保存下来


如果出现过

就要从出现的那个数之后开始重新找串


有没有出现都要判断当前串的长度是不是min

#include <iostream>#include <cstdio>#include <map>#define N 1000010using namespace std;map<int, int> vis;int num[N];int main(){int cas, n;scanf("%d", &cas);while (cas--) {scanf("%d", &n);vis.clear();for (int i = 1; i <= n; i++) scanf("%d", &num[i]);int ans = 0, cnt = 0;for (int i = 1; i <= n; i++) {if(!vis[num[i]]) {vis[num[i]] = i;cnt++;if (cnt > ans)ans = cnt;}else {if (cnt > ans)ans = cnt;i = vis[num[i]];vis.clear();cnt = 0;}}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击