题目:
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.
Input
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.
Output
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.
Example
Input
9 arrayhead 5 arya harry ray r areahydra
Output
5 6 5 2 9
题目大意:对于给出的每个字符串,取源字符串中的前几个才能组合成所给出的字符串,取出来的不一定全用。
思路:二分查找。
详见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
#define Max 99999999
using namespace std;
int n,m;
int mx,mi;
string a,b;
int mp[200005][27];//mp[i][j]表示源字符串中前i个字符有几个j字符。
int st[27];//st[i]表示字符i的个数
void erfen()
{
int mid;
mx=n;mi=1;
int i,j,k;
while(mi<=mx)
{
mid=(mx+mi)/2;
for(i=1;i<=26;i++)
{
if(mp[mid][i]<st[i])//要是前mid个字符不能包含所要找的字符,则答案在后面
{
mi=mid+1;
break;
}
}
if(i>26)//同理上面
{
mx=mid-1;
}
}
}
int main()
{
scanf("%d",&n);
cin>>a;
int i,j,k;
for(i=0;i<=n;i++)
{
for(j=0;j<27;j++)
{
mp[i][j]=0;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=26;j++)
mp[i][j]=mp[i-1][j];//注意累加关系
mp[i][a[i-1]-'a'+1]++;
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
cin>>a;
memset(st,0,sizeof(st));
for(j=0;a[j]!='\0';j++)
{
st[a[j]-'a'+1]++;
}
erfen();
printf("%d\n",mi);
}
return 0;
}