Set is used in Liu lujia's book,
Using the set to search for. Count () and delete. Erase (). This method is better than mine and takes a shorter time.
I think map can also complete this task, but it is not convenient to delete it. You need to first find the find () subscript and then delete the element corresponding to this subscript.
However, for map usage, the following method is easier to implement.
I thought it would be faster to calculate ans after reading this side, but in fact it is better to read and then use set to process Explain faster.
#include<cstdio>#include<iostream>#include<map>#include<string>#include<algorithm>using namespace std;map<string,int> ma;int main(){ int t,n; int ans=0; scanf("%d",&t); while(t--) { ans=0; scanf("%d",&n); ma.clear(); string s1; int L=0,R=0; for(int i=0;i<n;i++) { cin>>s1; if(ma.count(s1)&&ma[s1]>=L) { L=ma[s1]+1; } R++; ans=max(ans,R-L); ma[s1]=i; } printf("%d\n",ans); } return 0;}