0%

51Nod 1127 最短的包含字符串 滑窗算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

typedef long long LL;
using namespace std;

int main(int argc, char **argv) {
string str;
while(cin >> str) {
unordered_map<char, int> vis;
int ans = INT_MAX;
int l = 0, r = 0;
++vis[str[r]];
int num = 1;
while(l <= r && r < str.size()) {
if(num == 26) {
ans = min(ans, r - l + 1);
if(--vis[str[l++]] == 0) --num;
} else {
if(vis[str[++r]]++ == 0) ++num;
}
}
if(ans == INT_MAX) cout << "No Solution" << endl;
else cout << ans << endl;
}
return 0;
}

/**
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ

28
*/