description:
长度为n的字符串 m次询问 第i个位置上的字符在i之前出现几次
solution:
题意有点绕 只要用一个数组f[i] 来代表字符i在i之前出现多少次 数组标记字符出现多少次即可
也可以考虑离线操作
code:
#include <bits/stdc++.h> using namespace std; #define LL long long #define ULL unsigned long long #define mes(x, a) memset(x, a, sizeof(x)); #define sca(a) scanf("%d", &a) #define lowbit(x) x &(-x) #define mk make_pair #define pb(x) push_back(x) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define lson v << 1 #define rson v << 1 | 1 #define pii pair<int, int> inline void read(int &x) { x = 0; int flag_read = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') flag_read = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } x *= flag_read; } void out(int x) { if (x > 9) { out(x / 10); } putchar(x % 10 + '0'); } const int N = 1000005; char s[N]; int f[N],vis[35]; int main() { int n, m; scanf("%d%d", &n, &m); scanf("%s", s + 1); for(int i = 1;i <= n;i ++){ vis[s[i] - 'a'] ++ ; f[i] = vis[s[i] - 'a']; } while(m --){ int x; scanf("%d",&x); printf("%d\n",f[x]); } return 0; }