Codeforces Round #624 (Div. 3)C. Perform the Combo

Topic:
Give a string str of length n and a sequence p with m elements.
Str before count of the number of letters p [i] elements appear in
the final plus the number of all the letters appear str
number of outputs 26 letters appearing
ideas:
The original idea is to open a two-dimensional array to maintain the 26 letters The number can be accumulated and summed. But TLE

/*****************************
*author:ccf
*source:cf_round_624_C
*topic:
*******************************/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define ll long long
using namespace std;

const int N = 2e5+7;
int cas,n,m;
int cnt[200],p[N];
char str[N];
int ans[N][200];
int main(){
	//freopen("data.in","r",stdin);
	scanf("%d",&cas);
	while(cas--){
		memset(cnt,0,sizeof cnt);
		memset(ans,0,sizeof ans);
		scanf("%d %d",&n,&m);
		scanf("%s",str+1);
		
		for(int i = 1 ; i <= n; ++i){
			cnt[str[i]]++;
			for(char j = 'a'; j <= 'z';++j){
 				ans[i][j] = ans[i-1][j];
 				if(j == str[i]) ans[i][str[i]]++;
			}
		}
		for(int i = 1; i <= m; ++i){
			scanf("%d",p+i);
			for(char j = 'a'; j<= 'z';++j )
				cnt[j] += ans[p[i]][j];
		}
		for(char i = 'a'; i <= 'z'; ++i){
			printf("%d",cnt[i]);
			if(i == 'z') printf("\n");
			else printf(" ");
		}
	}
	return 0;
}

After the game, I learned a clever idea similar to "suffix and":
first record the number of occurrences of each stop position with an array cnt [p [i]].
tmp means the number of stoppages appearing when scanning to the i-th position
and then scans from back to front. If the i-th position stops (cnt [i]> 0), the number of occurrences will be accumulated, tmp + = cnt [p [i]], just count the number of times the letter appears at the end, ans [str [i]] + = tmp.
Take the first sample as an example:

4 2
abca
1 3
先将str所有的字母出现的次数先统计一遍:
ans[a] = 2,ans[b] = 1,ans[c] = 1
1,3号位置出现过一次cnt[1] = 1,cnt[3] = 1;

从后向前扫描:
扫到‘a’时,tmp = 0
扫到‘ac’时,tmp += cnt[3],tmp = 1,ans[c] += 1,ans[c] = 2;
扫到‘acb’时,tmp = 1,ans[b] += 1,ans[b] = 2;
扫到‘acb’时,tmp += 1,tmp = 2,ans[a] += 1,ans[a] = 4;
得出答案
/*****************************
*author:ccf
*source:cf_round_624_C
*topic:
*******************************/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define ll long long
using namespace std;

const int N = 2e5+7;
int n,m,cas;
char str[N];
int p[N];
int cnt[N],ans[N],tmp = 0;
void solve(){
	for(int i = n; i >= 1; --i){
		ans[str[i]]++;
		if(cnt[i]){
			tmp += cnt[i];
		}
		ans[str[i]] += tmp;
	}
	for(char i = 'a'; i <= 'z'; ++i){
		printf("%d ",ans[i]);
	}
	printf("\n");
}
int main(){
	//freopen("data.in","r",stdin);
	scanf("%d",&cas);
	while(cas--){
		tmp = 0;
		memset(ans,0,sizeof ans);
		memset(cnt,0,sizeof cnt);
		scanf("%d %d",&n,&m);
		scanf("%s",str+1);
		for(int i = 1; i <= m; ++i) scanf("%d",p+i),cnt[p[i]]++;
		solve();
	}
	return 0;
}

Published 141 original articles · praised 71 · 60,000+ views

Guess you like

Origin blog.csdn.net/sinat_40872274/article/details/104890149