C - k-DMC
Time Limit: 2.525 sec / Memory Limit: 1024 MB
Score : 600600 points
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:
Here S[a]S[a] is the aa-th character of the string SS. Indexing is zero-based, that is, 0≤a≤N−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 (0≤i≤Q−1)(0≤i≤Q−1).
Input is given from Standard Input in the following format:
NNSSQQk0k0 k1k1 ...... kQ−1kQ−1
Print QQ lines. The ii-th line should contain the kiki-DMC number of the string SS.
Copy
18
DWANGOMEDIACLUSTER
1
18
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.
Copy
18
DDDDDDMMMMMCCCCCCC
1
18
Copy
210
The number of triples can be calculated as 6×5×76×5×7.
Copy
54
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
3
20 30 40
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, c−a<kic−a
By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation".
Copy
30
DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC
4
5 10 15 20
Copy
10
52
110
140
解题报告:本题用暴力枚举是肯定超时的,有三重循环左右,本来打算用二分,结果还是不好实现。仔细观察一下发现了规律,以K长度为滑动窗口向后动,分别记录D,M,DM的数量。如果一个D要脱离滑动窗口,那么与DM就会少M的个数,DMC的数量不影响,因为可看做之前那个时刻的数量,所以只要动态增删D,M的数量控制DM的数量,然后遇到C直接加上DM的数量即可。这个过程应该分治来解决。
代码
#include
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=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<