int hash(char c){
return c-'A'+1;
}
bool judge_Same(int a[],int b[]){
for(int i=0;i<200;i++){
if(b[i]!=0 && b[i]>a[i]) return false;
}
return true ;
}
char* minWindow(char* s, char* t) {
int ns=strlen(s);
int ts=strlen(t);
char* tem=(char*)malloc(sizeof(char)*(ns+1));
for(int i=0;i<=ns;i++) tem[i]=0;
if(ns<ts) return tem;
if(strcmp(s,t)==0) return s;
if(ts==1){
for(int i=0;i<ns;i++){
if(s[i]==t[0]) return t;
}
return tem;
}
int head=0,tail=0;//区间始末
int a[200]={0};
int b[200]={0};
for(int i=0;i<ts;i++){
b[hash(t[i])]++;
}
int minhead=0,minn=ns+1;
a[hash(s[0])]++;
while(head<=tail && tail<ns){
if(!judge_Same(a,b)){
if(tail==ns-1) break;
tail++;
a[hash(s[tail])]++;
}else{
if(minn>tail-head+1){
minhead=head;
minn=tail-head+1;
}
a[hash(s[head++])]--;
while(b[hash(s[head])]==0){
a[hash(s[head++])]--;
}
}
}
if(minn!=ns+1){
for(int i=minhead;i<minhead+minn;i++){
tem[i-minhead]=s[i];
}
}
return tem;
}
结果:
标签:子串,head,hash,int,最小,char,76,return,ns From: https://www.cnblogs.com/llllmz/p/18038387