CodeForces - 1187B - Letters Shop

CF-1187B

B. Letters 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 s.

There are m friends, the i-th of them is named t i t_i ti. 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 s s s=“arrayhead” and t i t_i ti=“arya” 5 letters have to be bought (“arrayhead”). For example, for s=“arrayhead” and ti=“harry” 6 letters have to be bought (“arrayhead”). For example, for s=“arrayhead” and ti=“ray” 5 letters have to be bought (“arrayhead”). For example, for s=“arrayhead” and ti=“r” 2 letters have to be bought (“arrayhead”). For example, for s=“arrayhead” and ti=“areahydra” all 9 letters have to be bought (“arrayhead”). It is guaranteed that every friend can construct her/his name using the letters from the string s.

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 n (1≤ n ≤ 2⋅105) — the length of showcase string s.

The second line contains string s, consisting of exactly n lowercase Latin letters.

The third line contains one integer m (1 ≤ m ≤ 5⋅104) — the number of friends.

The i-th of the next m lines contains ti (1 ≤ | t i ∣ t_i| ti ≤ 2⋅105) — the name of the i-th friend.

It is guaranteed that ∑ i = 1 m ∣ t i ∣ \displaystyle \sum^{m}_{i=1}{|t_i|} i=1mti ≤ 2⋅105.

Output For each friend print the length of the shortest prefix of letters from s 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 s.

基本题意

1.输入一个数 n n n 代表字符串 s 1 s1 s1 的长度
2.输入字符串 s 1 s1 s1
3.输入一个数 t t t 代表测试组数, 每一组输入字符串 s 2 s2 s2
4.求 s 1 s1 s1中以s1[0]开头的子串长度最少为几的时候 , 其中的字母能包含 s 2 s2 s2中所有字母

思路

s 2 s2 s2中的字母一个个在 s 1 s1 s1中找, (每一次都从头找起)
记录 s 2 s2 s2中每一个字母在 s 1 s1 s1中出现的最初的位置(如果有了出现就直接标记,若之后有重复就跳过标记)
所得位置中的最大的那个数为ans

这是思路图

#include<string>
#include<algorithm>	//max头文件
#include<vector>	//不定长数组
using namespace std;
int main()
{
	int n, t;	// n为s1长度, t为输入组数
	cin >> n;
	string s1, s2;
	cin >> s1;
	vector <int> vec[26];	//a-z只有26个❤❤¤¤	记录每个字母的位置
	//PS: a-z 用 0-25 下标表示
	for (int i = 0; i < n; i++)
		vec[s1[i] - 'a'].push_back(i);	//第i个是(s1[i] - 'a')  øøøø 因为是ascii所以要 - 'a'
	cin >> t;	//输入组数
	while (t--) {
		cin >> s2;
		int ans = 0;	//可以默认更小	下标最小从0开始而已
		int flag[26] = {};	//标记数组, 用于记录是否有重复的	比如我给的图中 flag[r-'x'][0] = 1	, flag[r-'x'][1] = 2 (注意这里是下标,图中给的是位置)
		for (auto x : s2)	//(这里只是只读就可以这样子)
			ans = max(ans, vec[x - 'a'][flag[x - 'a']]), flag[x - 'a']++;	//(++很好理解)
		cout << ++ans << endl;	//(因为是下标,要得到位置要+1  †††† )
	}
	return 0;
}

运行时间和内存大小

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值