您的位置:首页 > 产品设计 > UI/UE

uva 11572 - Unique Snowflakes(Towpointer)

2014-01-31 23:03 417 查看
题目连接:uva 11572 - Unique Snowflakes

题目大意:给出一个字符串,找出最长的连续子串不含相同的数字。

解题思路:Towpointer,维护一个区间,保证没有相同的数字,同时维护最大长度。然后有因为数字比较大不能开数组直接记录,所以用map离散化。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1000005;

int n, g
;

map<int, int> v;

void init() {
v.clear();
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &g[i]);
}

int solve() {
int l = 0, ans = 0;
for (int r = 0; r < n; r++) {
v[g[r]]++;
while (v[g[r]] == 2) v[g[l++]]--;
ans = max(r - l + 1, ans);
}
return ans;
}

int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
printf("%d\n", solve());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: