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

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

I.e. if s=“abca”, m=2 and 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 t independent test cases.

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

Then t test cases follow.

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

The second line of each test case contains the string s consisting of n lowercase Latin letters.

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

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

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

Output
For each test case, print the answer — 26 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 9, ‘d’ is 4, ‘e’ is 5, ‘f’ is 3, ‘o’ is 9, ‘r’ is 3 and ‘s’ is 1.
思路:其实在草稿纸上画画思路就挺明白的了。这个题目,最本质就是求每一个位置数了多少次。但是每一个位置上有字母,因此我们可以计算每一个字母计算了多少次。我的做法是树状数组,但是我感觉差分也可以做。我们首先将字符串反转(这里要想明白),这样每一个位置也要变成n-a[i]+1了。然后套用树状数组的板子,求出每个位置数过的次数,换算到字母上就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=2e5+100;
int c[maxx],a[maxx],b[maxx],d[30];
string s;
int n,m;/*------树状数组------*/
inline int lowbit(int x){return x&-x;}
inline void add(int v)
{while(v<=n+10){c[v]+=1;v+=lowbit(v);}
}
inline int query(int v)
{int ans=0;while(v){ans+=c[v];v-=lowbit(v);}return ans;
}
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(int i=0;i<=n+10;i++) c[i]=0;memset(d,0,sizeof(d));cin>>s;for(int i=1;i<=m;i++) scanf("%d",&a[i]);a[m+1]=n;reverse(s.begin(),s.end());for(int i=1;i<=m+1;i++) add(n-a[i]+1);for(int i=1;i<=n;i++) b[i]=query(i);for(int i=0;i<n;i++) d[s[i]-'a']+=b[i+1];for(int i=0;i<26;i++) cout<<d[i]<<" ";cout<<endl;}return 0;
}

努力加油a啊,(o)/~

Perform the Combo CodeForces - 1311C(字符串反转+树状数组)相关推荐

  1. CodeForces - 1288E Messenger Simulator(树状数组)

    题目链接:点击查看 题目大意:给出n和m,表示n个人和m个操作,每次操作会将一个数x放到首位置上来,其他数往后顺延,比如: 初始时的数组为[4,1,5,3,2],当第一个x为 3 ,则序列变为[3,4 ...

  2. Codeforces Round #413 C-Fountains 树状数组

    题意 本题给我们给我们两种货币初始金额 每种货币分别对应能买不同种类的喷泉 每个喷泉有花费和价值 问我们如果单纯建造两个喷泉在限定金额内的最大值 分析 考虑一下 发现有三种情况 1 两个喷泉从coin ...

  3. Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树

    Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足  L >= li && R ...

  4. 【树状数组】CF961E Tufurama

    挺巧妙的数据结构题(不过据说这是一种套路? E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode ...

  5. Array Optimization by Deque (cf)树状数组

    原题链接:Problem - 1579E2 - Codeforces 今天学了树状数组,好牛,一个题想了一晚上才明白呜呜 1.首先这个题要想明白,每次插入一个数的时候,如果这个数插在队列的最前面,那么 ...

  6. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  7. C. Tyler and Strings(组合数学,树状数组维护前缀和)(Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad i)

    对我来说比较困难的一题了,尝试着自己写了一下,调不出来遂放弃. Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad in Info ...

  8. CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)

    CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段) 树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且 ...

  9. CodeForces - 1553F Pairwise Modulo(数论+树状数组)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,求 pk=∑1≤i,j≤kaimodajp_k = \sum_{1 \le i, j \le k} a_i \bmod a_jpk​=∑1 ...

最新文章

  1. 【计算机图形学】六面体旋转并实时切换虚线实线 - 代码实现
  2. 聚合复合_【专家视觉】聚合物接枝多壁碳纳米管及其聚氨酯复合材料
  3. 外贸网站制作 网页的宽度多少为合适
  4. OpenJudge/Poj 2001 Shortest Prefixes
  5. IOS Masonry自动布局
  6. SQL Server如何查看存储过程的执行计划
  7. 网络好不好,ping一下就知道
  8. 【好文收藏】k8s中Pod 无法正常解析域名:部署 DNS 调试工具排查
  9. 乐视android版本点四下,EUI5.9+Android7.0刷机包
  10. Spring @Order批注
  11. php文件多上传文件,php文件上传(多文件上传)
  12. ubuntu 18.04.1 使用心得
  13. [计算机网络] - DNS基础介绍
  14. Linux 学习记录 二 (文件的打包压缩).
  15. Jupyter Notebook中未显示Conda环境
  16. Mysql的key_len计算方法
  17. nfine配置oracle,nfine去后门版和数据库说明
  18. symantec backup exec 2010 oracle 12,Symantec Backup Exec 2010在Windows平台下Oracle备份详细配置步骤...
  19. 百度AI攻略:iOCR自定义模板功能
  20. 使用iptables-persistent永久保存iptables规则

热门文章

  1. IOS15瀑布流的使用
  2. pcm 采样率转换_PCM编码与Waveform音频文件(.wav)格式详解
  3. myeclipse springboot 运行内存溢出_springboot学习心得 - aowumao
  4. 23种设计模式详解_太厉害了,清华大牛一个坦克项目就把23种设计模式给抽丝剥茧了...
  5. 计算机图形学基础期末考试试题,计算机图形学基础_试卷(B)答案
  6. 请验证实例名称是否正确并且 sql server 已配置为允许远程连接_安装MySQL后,需要调整的10个性能配置项...
  7. java 7.0下载_Java jre 7.0
  8. linux内核函数kmalloc,Linux_Linux平台上几个常见内核内存分配函数,* kmallocPrototype:#incl - phpStudy...
  9. 1066. [SCOI2007]蜥蜴【最大流】
  10. 牵引力教育就业数据显示:很多大学毕业就等于失业?