[CF1311C] Perform the Combo

Solution

前缀和搞一下即可

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;
int T,n,m,p[N],a[N][26],ans[26];
char s[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--) {
        cin>>n>>m>>s+1;
        for(int i=1;i<=m;i++) cin>>p[i];
        ++m;
        p[m]=n;
        for(int i=1;i<=n;i++) {
            for(int j=0;j<26;j++) a[i][j]=a[i-1][j]+(s[i]-'a'==j);
        }
        for(int i=1;i<=m;i++) {
            for(int j=0;j<26;j++) ans[j]+=a[p[i]][j];
        }
        for(int i=0;i<26;i++) cout<<ans[i]<<" ";
        cout<<endl;
        memset(ans,0,sizeof ans);
        for(int i=1;i<=n;i++) {
            p[i]=ans[i]=s[i]=0;
            memset(a[i],0,sizeof a[i]);
        }
    }
}
原文地址:https://www.cnblogs.com/mollnn/p/12372882.html