码迷,mamicode.com
首页 > 其他好文 > 详细

C - Perform the Combo

时间:2020-03-13 19:02:07      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:span   pac   time   during   seq   Fix   put   wro   uri   

You want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in ss . I.e. if s=s= "abca" then you have to press ‘a‘, then ‘b‘, ‘c‘ and ‘a‘ again.

You know that you will spend mm wrong tries to perform the combo and during the ii -th try you will make a mistake right after pipi -th button (1pi<n1≤pi<n ) (i.e. you will press first pipi buttons right and start performing the combo from the beginning). It is guaranteed that during the m+1m+1 -th try you press all buttons right and finally perform the combo.

I.e. if s=s= "abca", m=2m=2 and p=[1,3]p=[1,3] then the sequence of pressed buttons will be ‘a‘ (here you‘re making a mistake and start performing the combo from the beginning), ‘a‘, ‘b‘, ‘c‘, (here you‘re making a mistake and start performing the combo from the beginning), ‘a‘ (note that at this point you will not perform the combo because of the mistake), ‘b‘, ‘c‘, ‘a‘.

Your task is to calculate for each button (letter) the number of times you‘ll press it.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t1041≤t≤104 ) — the number of test cases.

Then tt test cases follow.

The first line of each test case contains two integers nn and mm (2n21052≤n≤2⋅105 , 1m21051≤m≤2⋅105 ) — the length of ss and the number of tries correspondingly.

The second line of each test case contains the string ss consisting of nn lowercase Latin letters.

The third line of each test case contains mm integers p1,p2,,pmp1,p2,…,pm (1pi<n1≤pi<n ) — the number of characters pressed right during the ii -th try.

It is guaranteed that the sum of nn and the sum of mm both does not exceed 21052⋅105 (n2105∑n≤2⋅105 , m2105∑m≤2⋅105 ).

It is guaranteed that the answer for each letter does not exceed 21092⋅109 .

Output

For each test case, print the answer — 2626 integers: the number of times you press the button ‘a‘, the number of times you press the button ‘b‘, … , the number of times you press the button ‘z‘.

Example

Input
3
4 2
abca
1 3
10 5
codeforces
2 8 3 2 9
26 10
qwertyuioplkjhgfdsazxcvbnm
20 10 1 2 3 5 10 5 9 4
Output
4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 
2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2 

Note

The first test case is described in the problem statement. Wrong tries are "a", "abc" and the final try is "abca". The number of times you press ‘a‘ is 4 , ‘b‘ is 2 and ‘c‘ is 2 .

In the second test case, there are five wrong tries: "co", "codeforc", "cod", "co", "codeforce" and the final try is "codeforces". The number of times you press ‘c‘ is 99 , ‘d‘ is 4 , ‘e‘ is 5 , ‘f‘ is 3 , ‘o‘ is 9 , ‘r‘ is 3 and ‘s‘ is 1 .

 

题解:

Tutorial

1311C - Perform the Combo

We can consider all tries independently. During the ii-th try we press first pipi buttons, so it makes +1+1 on the prefix of length pipi. So the ii-th character of the string will be pressed (the number of piipi≥i plus 11) times. We can use sorting and some kind of binary search to find this number for each character but we also can build suffix sums to find all required numbers. We can build suffix sums using the following code:


vector<int> cnt(n);
for (int i = 0; i < m; ++i) {
++cnt[p[i]];
}
for (int i = n - 1; i > 0; –i) {
cnt[i - 1] += cnt[i];
}

So as you can see, the ii-th element of pp will add 11 in each position from 11 to pipi. So we got what we need. After that we can calculate the answer for each character in the following way:


vector<int> ans(26);
for (int i = 0; i < n; ++i) {
ans[s[i] - ‘a‘] += cnt[i] + 1;
}

Time complexity: O(nlogn)O(nlog?n) or O(n)O(n).

Solution

#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
	
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		string s;
		cin >> n >> m >> s;
		vector<int> pref(n);
		for (int i = 0; i < m; ++i) {
			int p;
			cin >> p;
			++pref[p - 1];
		}
		for (int i = n - 1; i > 0; --i) {
			pref[i - 1] += pref[i];
		}
		vector<int> ans(26);
		for (int i = 0; i < n; ++i) {
			ans[s[i] - ‘a‘] += pref[i];
			++ans[s[i] - ‘a‘];
		}
		for (int i = 0; i < 26; ++i) {
			cout << ans[i] << " ";
		}
		cout << endl;
	}
	
	return 0;
}

C - Perform the Combo

标签:span   pac   time   during   seq   Fix   put   wro   uri   

原文地址:https://www.cnblogs.com/lijiahui-123/p/12487852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!