http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1127
A map can be used to calculate the number of different characters from the beginning to the current position, moving toward the end of the interval, and if a A-Z is included, the shortest length can be updated, and then the first character is reduced by 1, which means moving forward.
This can be solved in nlogn time. It can be mapped either with a map or a one-dimensional array.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - #pragmaComment (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 510 - #defineMoD 1000000000 the using namespacestd; + Charstr[100010]; A intMain () the { + //Read (); -scanf"%s", str); $ intL=strlen (str); $map<Char,int>m; - ints=0, t=0, num=0, res=inf; - while(1) the { - while(t<l&&num< -)Wuyi { the if(m[str[t++]]++==0) num++; - //printf ("%d\n", num); Wu } - if(num< -) Break; AboutRes=min (res,t-s); $ if(--m[str[s++]]==0) num--; - } - if(Res==inf) printf ("No solution\n"); - Elseprintf"%d\n", res); A return 0; +}
51nod 1127 Shortest containing string (ruler)