问题标题: 洛谷:洛谷P1381 单词背诵怎么写

0
0

0
已采纳
黄昊轩
黄昊轩
新手守护
新手守护
map <string,int> mp;
  int n,m,a[100005],p1,p2,buf[1005],buf0[1005],ans=1e+9,cnt=0;
  string str;

  inline bool checker() {
      for(int i=1;i<=n;i++) 
         if(buf[i]==0 && buf0[i]) 
             return 0;
     return 1;
 }

 int main(){
     ios::sync_with_stdio(false);
     cin>>n;
     for(int i=1;i<=n;i++) cin>>str, mp.insert(make_pair(str,i));
     cin>>m;
     for(int i=1;i<=m;i++) {
         cin>>str;
         if(mp.find(str)!=mp.end())
             a[i]=mp.find(str)->second, buf[a[i]]++;
    }
     for(int i=1;i<=n;i++) cnt+=buf[i]!=0, buf0[i]=buf[i];
     memset(buf,0,sizeof buf);
     p1=1;p2=0;
     while(p1<=m) {
         while(!checker() && p2<=m) p2++, buf[a[p2]]++;
         if(checker() && p1<=p2) 
             ans=min(ans,p2-p1+1);
         buf[a[p1]]--; p1++;
     }
     cout<<cnt<<endl<<(cnt==0?0:ans)<<endl;
0
我要回答