C - Perform the Combo
C - Perform the Combo
思路:当读到这个题的时候,第一反应就是枚举,但是,无线超时,没办法,那就变,利用前缀和,减少时间。
代码:
#include<iostream> #include<string> #include<algorithm> #define MAX 200005 using namespace std; long long str[MAX]; long long b[28]; char s[MAX]; int main(){ int t,n, m; scanf("%d", &t); while (t--){ memset(str, 0, sizeof str); memset(b, 0, sizeof b); scanf("%d%d", &n, &m); scanf("%s", s + 1); for (int i = 1; i <= m; ++i){ long long a; scanf("%lld", &a); str[a] ++; } for (int i = n - 1; i >= 1; i--) str[i] += str[i + 1]; for (int i = 1; i <= n; ++i){ b[s[i] - 'a'] += str[i] + 1; } for (int i = 0; i < 26; ++i) { printf("%lld ", b[i]); } printf("\n"); } return 0; }