输出最小长度,如果达不到要求输出-1
6
尺取法,作者个体的时候,不知道什么事尺取法,但是思路差不多。但是没有过。
后来百度看别人的代码,自己又写了一遍。
相当于两个指针扫了一遍,
我的K指针为前指针,for循环的i为后指针。
没次判断能否挪动前指针。记录最小长度。
实践证明,,这个要比二分的快。
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <stack> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define LL long long #define N 10010 #define mod 1000000007 char a[N]; int need[30],have[30]; int judge() { for(int j=0;j<26;j++) if(have[j]<need[j]) return 0; return 1; } int main() { int T,n,m; scanf("%d",&T); while(T--) { memset(have,0,sizeof(have)); memset(need,0,sizeof(need)); scanf("%d%d",&n,&m); scanf("%s",a); char c,t; int ans=99999999,k=0,nu,d; for(int i=0;i<m;i++) { scanf("%c%c%d",&c,&t,&d); need[t-'a']=d; } for(int i=0;i<n;i++) { nu=a[i]-'a'; have[nu]++; if(judge()) while(judge()) { ans=min(ans,i-k+1); int temp=a[k]-'a'; have[temp]--; k++; } } if(ans==99999999) printf("-1\n"); else printf("%d\n",ans); } return 0; }
