Meaning of the questions: Given a string s, s character can take away from left to right, and at least until the first few s position to spell t
Ideas: prefix record two-dimensional array, and then half can be.
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define inf 5000000 5 #define N 210050 6 int sum[N][30]; 7 int arr[30]; 8 string s; 9 string str; 10 int n; 11 bool slove(int id) { 12 for(int i=0; i<26; i++) 13 if(arr[i]-sum[id][i]>0) 14 return false; 15 return true; 16 } 17 int main() { 18 cin>>n; 19 cin>>str; 20 int _; 21 cin>>_; 22 for(int i=0; i<str.size(); i++) { 23 ++sum[i][str[i]-'a']; 24 if(i!=0) { 25 for(int j=0; j<26; j++) { 26 sum[i][j]+=sum[i-1][j]; 27 } 28 } 29 } 30 while(_--) { 31 cin>>s; 32 for(int i=0; i<=26; i++) 33 arr[i]=0; 34 for(int i=0; i<s.size(); i++) { 35 arr[s[i]-'a']++; 36 } 37 int l=0; 38 int r=n; 39 int ans=inf; 40 while(l<=r) { 41 int mid=(l+r)/2; 42 if(slove(mid)) { 43 r=mid-1; 44 ans=min(ans,mid); 45 } else { 46 l=mid+1; 47 } 48 } 49 cout<<ans+1<<'\n'; 50 } 51 return 0; 52 }