您的位置:首页 > 其它

Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)

2016-07-23 14:19 369 查看
要维护区间内字符数为定值,求最小的满足条件的区间典型的双指针。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <map>
using namespace std;
#define pr(x) cout << #x << ": " << x << "  "
#define pl(x) cout << #x << ": " << x << endl;

struct jibancanyang
{
int n, t;
string str;

void two() {
int l = 0, r = 0;
map<char, int> st;
int ret = int(1e9);
while (l < n) {
while (r < n && (int)st.size() < t) {
st[str[r++]]++;
}
if (t <= (int)st.size()) ret = min(ret, r - l);
else break;
st[str[l]]--;
if (st[str[l]] == 0) st.erase(str[l]);
l++;
}
cout << ret << endl;
}

void run() {
cin >> n >> str;
set<char> st;
for (int i = 0; i < (int)str.size(); i++) {
st.insert(str[i]);
}
t = st.size();
two();
}

}ac;

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
ac.run();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: