题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1127
思路:尺取法,一开始我考虑更新右指针,直到遇到一个和l指针指向的字符相同的时候为止,发现这样做ac不了。于是换了一个思路。
一直更新r指针,直到所有字符都出现了一遍后,更新答案和左指针,导致有一个缺口,这时候再更新r指针。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%lld", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onenum(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 const int maxn = 100100; 72 int ascii[256]; 73 char s[maxn]; 74 int n; 75 76 int main() { 77 // FRead(); 78 while(~Rs(s)) { 79 Cls(ascii); 80 n = strlen(s); 81 if(n < 26) { 82 printf("No Solution\n"); 83 continue; 84 } 85 int ret = 0x7f7f7f; 86 int cnt = 0; 87 int l = 0, r = 1; 88 ascii[s[l]]++; cnt = 1; 89 while(r < n) { 90 if(ascii[s[r]] == 0) cnt++; 91 ascii[s[r]]++; 92 while(cnt == 26) { 93 ret = min(ret, r-l+1); 94 ascii[s[l]]--; 95 if(ascii[s[l]] == 0) cnt--; 96 l++; 97 } 98 r++; 99 } 100 if(ret == 0x7f7f7f) printf("No Solution\n"); 101 else printf("%d\n", ret); 102 } 103 RT 0; 104 }
时间: 2024-10-14 21:10:28