The letters shop showcase is a string ss, consisting of nn lowercase Latin letters. As the name tells, letters are sold in the shop.
Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string ss.
There are mm friends, the ii-th of them is named titi. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount.
- For example, for ss="arrayhead" and titi="arya" 55 letters have to be bought ("arrayhead").
- For example, for ss="arrayhead" and titi="harry" 66 letters have to be bought ("arrayhead").
- For example, for ss="arrayhead" and titi="ray" 55 letters have to be bought ("arrayhead").
- For example, for ss="arrayhead" and titi="r" 22 letters have to be bought ("arrayhead").
- For example, for ss="arrayhead" and titi="areahydra" all 99 letters have to be bought ("arrayhead").
It is guaranteed that every friend can construct her/his name using the letters from the string ss.
Note that the values for friends are independent, friends are only estimating them but not actually buying the letters.
The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of showcase string ss.
The second line contains string ss, consisting of exactly nn lowercase Latin letters.
The third line contains one integer mm (1≤m≤5⋅1041≤m≤5⋅104) — the number of friends.
The ii-th of the next mm lines contains titi (1≤|ti|≤2⋅1051≤|ti|≤2⋅105) — the name of the ii-th friend.
It is guaranteed that ∑i=1m|ti|≤2⋅105∑i=1m|ti|≤2⋅105.
For each friend print the length of the shortest prefix of letters from ss s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount.
It is guaranteed that every friend can construct her/his name using the letters from the string ss.
9 arrayhead 5 arya harry ray r areahydra
5 6 5 2 9
http://codeforces.com/problemset/problem/1187/B
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<vector> #include<cmath> #include<string> #include<map> #include<queue> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; vector<ll> p[27]; ll a[27]; int main(){ ll n; cin>>n; string s; cin>>s; for(int i=0;i<n;i++){ p[s[i]-'a'].push_back(i);//存储每个字母对应的下标,按顺序 } ll m; cin>>m; while(m--){ memset(a,0,sizeof(a));//存储子串中字母出现的次数 string z; cin>>z; ll len=z.size(),ans=0; for(int i=0;i<len;i++){ ans=max(ans,p[z[i]-'a'][a[z[i]-'a']]+1); a[z[i]-'a']++; } cout<<ans<<endl; } return 0; }