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. 在windowx的Hyper-v 安装CentOS系统

    博客写的很少,一方面是因为我觉得目前很多博客都是相互抄袭,或者有很多部分都是重复的内容.而我自己再去写同样的内容的画,有点浪费时间. 所以,如果我要写,我希望是写一些与众不同,或者重复率比较低的内容, ...

  2. 在django中用MySQL为数据库 新建一个项目的流程

    创建mysql数据库 在cmd输入 mysql -uroot -p 启动mysql客户端并连接服务器 创建一个db_orm的数据库 mysql>CREATE DATABASE db_orm ch ...

  3. 你不得不知的几个互联网ID生成器方案

    服务化.分布式已成为当下系统开发的首选,高并发操作在数据存储时,需要一套id生成器服务,来保证分布式情况下全局唯一性,以确保系统的订单创建.交易支付等场景下数据的唯一性,否则将造成不可估量的损失. 基 ...

  4. ElasticStack学习(七):ElasticSearch之Mapping初探

    一.Mapping的概念 1.Mapping类似于数据库中的Schema的定义,作用如下: 1)定义索引中的字段的名称: 2)定义字段的数据类型,例如字符串.数字.日期.布尔等: 3)对每个字段进行倒 ...

  5. shell_chmod与目录权限

    此篇文档将讲解关于linux中文件权限常用命令chmod.为了达到一个比较好的效果,我会在需要的地方实际上机验证测试,并截图给朋友们看.我的linux机器装的是(opensuse-11.3),并且以文 ...

  6. MyBatis从入门到精通:第二章数据的创建与插入文件

    数据库表的创建: create table sys_user ( id bigint not null auto_increment, ), user_password ), user_email ) ...

  7. 关于使用 AJax 生成Form表单,且表单提交需要验证,验证实效的解决方法

    @Ajax.ActionLink("添加", "AddUser",new AjaxOptions() {InsertionMode = InsertionMod ...

  8. c++小游戏:洛谷彩票

    #include <cstdlib> #include <iostream> #include <cstdio> #include <cmath> #i ...

  9. Button事件的三种实现方法

    onclick事件的定义方法,分为三种,分别为在xml中进行指定方法:在Actitivy中new出一个OnClickListenner():实现OnClickListener接口三种方式. 1.在xm ...

  10. 个人永久性免费-Excel催化剂功能第61波-快速锁定解锁单元格及显示隐藏公式

    Excel的所有功能都是需求导向的,正因为有客户在企业管理的过程中,有这样的需求出现了,然后相应的Excel就出现了相应的功能来辅助管理,学习Excel的功能,其实真的可以学习到先进企业的许多的管理思 ...