2016 Weak School Alliance 11 session 10.5 --- As Easy As Possible (doubled), 201610.5 ---
Question Link
Https://acm.bnu.edu.cn/v3/contest_show.php? Cid = 8506 # problem/
Problem description
As we know, the NTU Final PK contest usually tends to be pretty hard. deleteams got frustrated when participant ipating NTU Final PK contest. so I decide to make the first problem as "easy" as possible. but how to know how easy is a problem? To make our life easier, we just consider how easy is a string. here, we introduce a sane definition of "easiness ". the easiness of a string is the maximum times of "easy" as a subsequence of it. for example, the easiness of "eeaseyaesasyy" is 2. since "easyeasy" is a subsequence of it, but "easyeasyeasy" is too easy. how to calculate easiness seems to be very easy. so here is a string s consists of only 'E', 'A', 's', and 'y '. please answer m queries. the I-th query is a interval [li, ri], and please calculate the easiness of s [li .. ri].
Input
The first line contains a string s. the second line contains an integer m. each of following m lines contains two integers li, ri. • 1 ≤ | s | ≤ 105 • 1 ≤ m ≤ 105 • 1 ≤ li ≤ ri ≤ | s | • s consists of only 'E', 'A ', 'S ', and 'y'
Output
For each query, please output the easiness of that substring in one line.
Examples
Standard Input
Easy
3
1 4
2 4
1 3
Eeaseyaesasyy
4
1 13
2 12
2 10
3 11
Standard output
1
0
0
2
2
1
0
Question: I gave a string containing only 'e' a's 'y' and then asked m times, input l r each time to find the number of "easy" sequences in this interval (each "easy" character does not need to be connected together );
Train of Thought: Use the multiplication idea. Each vertex only records the position of the letter closest to it on its left, such as 'y' in front of record 'y ', 'A' record 'E', 's' record 'A', 'y' record 's' and note that the record is closest to I (y on the left) the position of y (stored in p [I]) defines anc [I] [j] to indicate the position of the (1 <j) character before the I character, this can achieve anc [I] [j] = anc [anc [I] [J-1] [J-1], query, find v = p [r], find the number of valid characters on the left, and divide it by 4;
The Code is as follows:
#include <iostream>#include <algorithm>#include <stdio.h>#include <cstring>#include <queue>using namespace std;typedef long long LL;char s[100005];int a[100005],p[100005];int anc[100005][21];int mp[4];int main(){ int m; while(scanf("%s",s+1)!=EOF) { int len=strlen(s+1); for(int i=1;i<=len;i++) { if(s[i]=='e') a[i]=0; else if(s[i]=='a') a[i]=1; else if(s[i]=='s') a[i]=2; else a[i]=3; } memset(mp,0,sizeof(mp)); for(int i=1;i<=len;i++) { int pre=(a[i]-1+4)%4; anc[i][0]=mp[pre]; mp[a[i]]=i; p[i]=mp[3]; } for(int i=1;i<=20;i++) { for(int j=1;j<=len;j++) { anc[j][i]=anc[anc[j][i-1]][i-1]; } } scanf("%d",&m); while(m--) { int l,r; int sum=1; scanf("%d%d",&l,&r); int v=p[r]; for(int i=20;i>=0;i--) { if(anc[v][i]>=l){ sum+=(1<<i); v=anc[v][i]; } } printf("%d\n",sum/4); } } return 0;}