codeforces 701C They Are Everywhere
问:包含全部种类的连续字符串的最短长度是多少。
写完了才知道写的是双指针。叫two pointer。
以前也写过,今天算是存个档吧~。
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <iostream> #include <vector> #include <cmath> #include <map> #include <string> #include <stack> #include <queue> using namespace std; int s_pos[100100]; int e_pos[100100]; char a[100100]; int kind[200]; int akind[200]; int ak=0; int ans=0x7f7f7f7f; int main() { int n; scanf ("%d",&n); scanf ("%s",a); int s_k=0,l=strlen(a); s_pos[s_k++]=0; for (int i=1; i<l; i++) { if (a[i]!=a[i-1]) s_pos[s_k++]=i; } int e_k=0; for (int i=0; i<l-1; i++) { if (a[i]!=a[i+1]) e_pos[e_k++]=i; } e_pos[e_k++]=l-1; for (int i=0; i<s_k; i++) { if (akind[a[s_pos[i]]]==0) { akind[a[s_pos[i]]]=1; ak++; } } if (ak==1) { printf ("1\n"); return 0; } int sp=0,ep=-1,tk=0; while (tk!=ak) { ep++; if (kind[a[s_pos[ep]]]==0) tk++; kind[a[s_pos[ep]]]++; } ans=min(ans,s_pos[ep]-e_pos[sp]+1); while (sp<s_k&&ep<e_k) { kind[a[e_pos[sp]]]--; sp++; if (kind[a[e_pos[sp-1]]]==0) { tk--; while (tk!=ak) { ep++; if (ep==e_k) break; if (kind[a[s_pos[ep]]]==0) tk++; kind[a[s_pos[ep]]]++; } if (ep==e_k) break; else ans=min(ans,s_pos[ep]-e_pos[sp]+1); } else { ans=min(ans,s_pos[ep]-e_pos[sp]+1); } } printf ("%d\n",ans); return 0; }