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 (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

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[];
ll a[];
int main(){
ll n;
cin>>n;
string s;
cin>>s;
for(int i=;i<n;i++){
p[s[i]-'a'].push_back(i);//存储每个字母对应的下标,按顺序
}
ll m;
cin>>m;
while(m--){
memset(a,,sizeof(a));//存储子串中字母出现的次数
string z;
cin>>z;
ll len=z.size(),ans=;
for(int i=;i<len;i++){
ans=max(ans,p[z[i]-'a'][a[z[i]-'a']]+);
a[z[i]-'a']++;
}
cout<<ans<<endl;
}
return ;
}

Letters Shop的更多相关文章

  1. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

  2. Codeforces Educational Codeforces Round 67

    目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sortin ...

  3. Codeforces Edu Round 67 A-C + E

    A. Stickers and Toys 考虑尽量先买\(max(s, t)\)个里面单独的.那么如果\(s + t > n\)那么\(s + t - n\)的部分就该把\(min(s, t)\ ...

  4. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  5. codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  6. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  7. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  8. poj1157LITTLE SHOP OF FLOWERS

    Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...

  9. Magicodes.Shop——版本历史

    Magicodes.Shop为湖南心莱信息科技有限公司(xin-lai.com)Magicodes系列产品之一. 产品中引用的Magicodes系列Nuget包的开源库地址为:https://gith ...

随机推荐

  1. 并发编程-concurrent指南-阻塞双端队列BlockingDeque

    java.util.concurrent 包里的 BlockingDeque 接口表示一个线程安放入和提取实例的双端队列. BlockingDeque 类是一个双端队列,在不能够插入元素时,它将阻塞住 ...

  2. HDU 1561:The more, The Better(有依赖的树型背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1561 题意:有n个点,容量为m,每个点有一个价值,还给出n条边,代表选第i个点之前必须先选ai,问最多的价值能取 ...

  3. 客户端持久化数据库---indexedDB使用

    _ 阅读目录 一:什么是indexedDB数据库? 二:IndexedDB数据库操作 2.1 打开或创建数据库 2.2 创建对象仓库(或叫创建表) 2.3 创建索引 2.4 新增数据 2.5 读取数据 ...

  4. 探究Hybrid-APP技术原理

    探究Hybrid-APP技术原理 author: @TiffanysBear 背景 随着Web技术的发展和移动互联网的发展,Hybrid技术已经成为一种前端开发的主流技术方案.那什么是Hybrid A ...

  5. 利用jenkins实现自动构建、部署,提升团队开发效率

    一大早就被群里的同学刷银川下雪的消息,看着我都发冷,突觉一阵凉风裹身,是不是该考虑秋裤了. 偏离主题,正文走起...... 使用jenkins目标:利用其结合maven完成自动构建,并部署到tomca ...

  6. 几款常用的在线API管理工具(是时候抛弃office编写接口文档了)

    在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...

  7. 十代雅阁广东车友群,雅阁广州车友群,深圳雅阁车友群,雅阁微信群、雅阁车友群、十代雅阁交流微信QQ群

    最近一直在关注第十代雅阁,不论是普通汽油版本还是油电混动版本都很不错,在网上看到很多评测文章和视频 后续都会整理发布到微信群中. 由于论坛发帖,博客发文都不是很方便,为了及时沟通,先创建了微信群,方便 ...

  8. [HNOI2015]菜肴制作 题解(自带口胡证明)

    [HNOI2015]菜肴制作 时间限制: 1 Sec  内存限制: 512 MB 题目描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为 ...

  9. CF39D Cubical Planet-C++

    银河系中没有你找不到的东西!有一颗形状为立方体的的行星正在绕着一颗形状为二十面体的恒星运转.现在我们让这颗行星的两个在同一条体对角线上的顶点置于(0,0,0)和(1,1,1)上.有两只苍蝇住在行星上. ...

  10. opatch卸载weblogic12.1.3.0补丁

    1.首先进入opatch目录 2.查看opatch命令(心急于卸载的朋友直接跳过这步) [weblogic@localhost OPatch]$ ./opatch -help Oracle 中间补丁程 ...