Letters Shop

B. Letters Shop
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1n21051≤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 (1m51041≤m≤5⋅104) — the number of friends.

The ii-th of the next mm lines contains titi (1|ti|21051≤|ti|≤2⋅105) — the name of the ii-th friend.

It is guaranteed that i=1m|ti|2105∑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
Copy
9
arrayhead
5
arya
harry
ray
r
areahydra
output
Copy
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; }

转载于:https://www.cnblogs.com/akpower/p/11285080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值