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
  1. 9
  2. arrayhead
  3. 5
  4. arya
  5. harry
  6. ray
  7. r
  8. areahydra
output

Copy
  1. 5
  2. 6
  3. 5
  4. 2
  5. 9
  1. http://codeforces.com/problemset/problem/1187/B
    #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string.h>
  5. #include<vector>
  6. #include<cmath>
  7. #include<string>
  8. #include<map>
  9. #include<queue>
  10. using namespace std;
  11. #define INF 0x3f3f3f3f
  12. typedef long long ll;
  13.  
  14. vector<ll> p[];
  15. ll a[];
  16. int main(){
  17. ll n;
  18. cin>>n;
  19. string s;
  20. cin>>s;
  21. for(int i=;i<n;i++){
  22. p[s[i]-'a'].push_back(i);//存储每个字母对应的下标,按顺序
  23. }
  24. ll m;
  25. cin>>m;
  26. while(m--){
  27. memset(a,,sizeof(a));//存储子串中字母出现的次数
  28. string z;
  29. cin>>z;
  30. ll len=z.size(),ans=;
  31. for(int i=;i<len;i++){
  32. ans=max(ans,p[z[i]-'a'][a[z[i]-'a']]+);
  33. a[z[i]-'a']++;
  34. }
  35. cout<<ans<<endl;
  36. }
  37. return ;
  38. }

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. POI自动调整列宽支持中文

    /** * @Description:表格自适应宽度(中文支持) * @Author: * @param sheet sheet * @param columnLength 列数 */ private ...

  2. 数据结构与算法分析(C++)读书笔记

    第一章 综述 数学知识复习: 指数.对数.级数.模运算.证明方法(证明数据结构分析几轮最常用方法:归纳法.反证法) 递归简论 C++知识:类.细节.模板.矩阵.

  3. 聚类时的轮廓系数评价和inertia_

    在进行聚类分析时,机器学习库中提供了kmeans++算法帮助训练,然而,根据不同的问题,需要寻找不同的超参数,即寻找最佳的K值 最近使用机器学习包里两个内部评价聚类效果的方法:clf=KMeans(n ...

  4. 寻觅Azure上的Athena和BigQuery (二):神奇的PolyBase

    前情回顾 在“数据湖”概念与理论逐渐深入人心的今天,面向云存储的交互式查询这个需求场景显得愈发重要.这是因为原生的云存储(主要指S3这样的对象存储)既能够容纳大容量的明细数据,又能在性能和成本间取得一 ...

  5. Spring 核心技术(3)

    接上篇:Spring 核心技术(2) version 5.1.8.RELEASE 1.4 依赖 典型的企业应用程序不会只包含单个对象(或 Spring 术语中的 bean).即使是最简单的应用程序也是 ...

  6. Spring Cloud Alibaba | Nacos服务中心初探

    目录 Spring Cloud Alibaba | Nacos服务中心初探 1. 什么是Nacos? 1.1 Nacos 1.0 1.2 Nacos 2.0 2. Nacos 架构及概念 2.1 服务 ...

  7. Java学习笔记之---单例模型

    Java学习笔记之---单例模型 单例模型分为:饿汉式,懒汉式 (一)要点 1.某个类只能有一个实例 2.必须自行创建实例 3.必须自行向整个系统提供这个实例 (二)实现 1.只提供私有的构造方法 2 ...

  8. 为什么说是时候拥抱.NET CORE了?

    微软和社区已经做了大量艰苦的工作,使.Net Core成为市场上具有竞争力的框架,帮助开发人员快速开发具有最佳性能和可扩展性的强大应用程序.做的最棒的事情是.Net Framework开发人员不需要任 ...

  9. CentOS 7离线安装Ansible

    前言 我一直都想成为自动化运维界最亮的仔,奈何自己实力不允许.不过,我一直都在奋斗的路上:这不,最近就在学习自动化运维界的神器--Ansible. 要系统的学习一下Ansible,那就是要先搭建学习环 ...

  10. 前端框架——树形结构Ztree的使用

    地址 官网:http://ztree.me 码云:https://gitee.com/zTree/zTree_v3 可以实现效果 使用方式 下载资源文件,引入到自己的项目中 <head> ...