#include<stdio.h> using namespace std;   int main(){ char s[]={'t','b','c','a','c','b','d','a','t','a'}; char t[]={'a','b','c','d'}; int s_len=10,t_len=4; int res; if(s_len<t_len){ res=-1; printf("%d",res); return 0; } //申请一个散列表,记录窗口中元素的情况  int hash[26]={0}; for(int i=0;i<t_len;++i){ ++hash[t[i]-'a']; } int l=0,count=0; for(int r=0;r<s_len;++r){ --hash[s[r]-'a']; if(hash[s[r]-'a']>=0){ //s[r]处的字符在t中  ++count; } //向右移动左指针           if(r>t_len-1) {              ++hash[s[l]-'a'];              if (hash[s[l]-'a']>0) --count;              ++l;          }          if(count==t_len && r-l+1==t_len){ res=l; printf("%d",res); return 0; } } res=-1;//没有找到 printf("%d",res); return 0; } 

牛客热帖

牛客网
牛客企业服务