Atcoder Dwango Programming Contest V C - k-DMC

C - k-DMC

 

Time Limit: 2.525 sec / Memory Limit: 1024 MB

Score : 600600 points

Problem Statement

In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short.
The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string.

Given a string SS of length NN and an integer kk (k≥3)(k≥3), he defines the kk-DMC number of SS as the number of triples (a,b,c)(a,b,c) of integers that satisfy the following conditions:

  • 0a<b<cN−10≤a<b<c≤N−1
  • S[a]S[a] = D
  • S[b]S[b] = M
  • S[c]S[c] = C
  • ca<kc−a<k

Here S[a]S[a] is the aa-th character of the string SS. Indexing is zero-based, that is, 0aN−10≤a≤N−1 holds.

For a string SS and QQ integers k0,k1,...,kQ−1k0,k1,...,kQ−1, calculate the kiki-DMC number of SS for each ii (0iQ−1)(0≤i≤Q−1).

Constraints

  • 3N≤1063≤N≤106
  • SS consists of uppercase English letters
  • 1Q≤751≤Q≤75
  • 3kiN3≤ki≤N
  • All numbers given in input are integers

 

Input

Input is given from Standard Input in the following format:

NNSSQQk0k0 k1k1 ...... kQ−1kQ−1

Output

Print QQ lines. The ii-th line should contain the kiki-DMC number of the string SS.

 

Sample Input 1 Copy

Copy

18

DWANGOMEDIACLUSTER

1

18

Sample Output 1 Copy

Copy

1

(a,b,c)=(0,6,11)(a,b,c)=(0,6,11) satisfies the conditions.
Strangely, Dwango Media Cluster does not have so much DMC-ness by his definition.

 

Sample Input 2 Copy

Copy

18

DDDDDDMMMMMCCCCCCC

1

18

Sample Output 2 Copy

Copy

210

The number of triples can be calculated as 6×5×76×5×7.

 

Sample Input 3 Copy

Copy

54

DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED

3

20 30 40

Sample Output 3 Copy

Copy

0

1

2

(a,b,c)=(0,23,36),(8,23,36)(a,b,c)=(0,23,36),(8,23,36) satisfy the conditions except the last one, namely, ca<kic−a<ki.
By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation".

 

Sample Output 4 Copy

Copy

30

DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC

4

5 10 15 20

Sample Output 4 Copy

Copy

10

52

110

140

解题报告:本题用暴力枚举是肯定超时的,有三重循环左右,本来打算用二分,结果还是不好实现。仔细观察一下发现了规律,以K长度为滑动窗口向后动,分别记录D,M,DM的数量。如果一个D要脱离滑动窗口,那么与DM就会少M的个数,DMC的数量不影响,因为可看做之前那个时刻的数量,所以只要动态增删D,M的数量控制DM的数量,然后遇到C直接加上DM的数量即可。这个过程应该分治来解决。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
	LL n,a[1000]={0},q;
	 string s;
	 cin>>n;
	 cin>>s;
	 cin>>q;
	 while(q--)
	 {LL DMnum=0,Dnum=0,Mnum=0,DMCnum=0,k;
	 cin>>k;
	 	  for(LL i=0;i<s.length();i++)
	  {if(i-k>=0&&s[i-k]=='D')
		   {
		   	Dnum--;
		   	DMnum-=Mnum;
		   }
		   if(i-k>=0&&s[i-k]=='M')
		   {
		   	Mnum--;
		   	
		   }
		   
	  	 if(s[i]=='D')Dnum++;
	  	 if(s[i]=='M')
	  	 {
	  	 	Mnum++;
	  	 	DMnum+=Dnum;
		   }
		   if(s[i]=='C')
		   {
		   	DMCnum+=DMnum;
		   }
		   
	  	 
	  }
	  cout<<DMCnum<<"\n";
	 }
	
}

猜你喜欢

转载自blog.csdn.net/explodee/article/details/84783043