弱校连萌 2016 国庆训练计划 10,5 Problem A. As Easy As Possible
2016-10-06 20:00
363 查看
【题解参考blog】http://www.cnblogs.com/chen9510/p/5934105.html
【题意】给了一个只含有'e' 'a' 's' 'y' 的字符串然后m次询问,每次询问输入l r 求这个区间含有多少个“easy”序列(每个“easy” 字符之间不需要连在一起)。
【解题方法】倍增法。每个点只记录最靠近它的在它左边的那个字母的位置,比如'e'记录前面的'y','a'记录前面的'e','s'记录前面的'a','y'记录前面的's' 并注意记录距离i最近(左边的y)的y的位置(用p[i]存储) 定义anc[i][j] 表示第i个字符前的第(1<<j)个字符的位置,这个可以用倍增做到 anc[i][j]=anc[anc[i][j-1]][j-1], 查询时,先找到tmp=p[r] 然后找左边有效字符个数,最后除以4 就是结果。注意查询的时候答案初始化为1,因为从y开始查找的。
【代码君】
// //Created by just_sort 2016/10/5 //Copyright (c) 2016 just_sort.All Rights Reserved // #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 1e5+7; char str[maxn]; int a[maxn],p[maxn]; int anc[maxn][22]; int main() { while(scanf("%s",str+1)!=EOF) { int len = strlen(str+1); for(int i = 1; i <= len; i++) { if(str[i] == 'e') a[i] = 0; else if(str[i] == 'a') a[i] = 1; else if(str[i] == 's') a[i] = 2; else{ a[i] = 3; } } int id[4]; memset(id,0,sizeof(id)); for(int i = 1; i <= len; i++) { int pre = (a[i]+3)%4; anc[i][0] = id[pre]; id[a[i]] = i; p[i] = id[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]; } } int q; scanf("%d",&q); while(q--) { int l,r; scanf("%d%d",&l,&r); if(l > r) swap(l,r); int ans = 1; int tmp = p[r]; for(int i = 20; i >= 0; i--){ if(anc[tmp][i] >= l) { ans += (1<<i); tmp = anc[tmp][i]; } } printf("%d\n",ans/4); } } return 0; }
相关文章推荐
- 【分块】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem A. As Easy As Possible
- as easy problem
- the method of resolve the problem that easyuefi can not select other hard disk as a bootable disk
- As Easy As Possible 倍增法
- Codeforces Round #364 (Div. 2), problem: (D) As Fast As Possible
- 2015-2016 ACM-ICPC Southwestern Europe Regional Contest (SWERC 15)训练总结【7/10】
- As Easy As Possible
- 2016多校训练Contest6: 1010 Windows 10 hdu5802
- BNUOJ 52317 As Easy As possible
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
- BNUOJ 52317 As Easy As Possible 树上倍增/主席树
- 2016 Multi-University Training Contest 10(2016多校训练第十场)1011
- BNUOJ52317 As Easy As Possible(树上倍增)
- 【模拟】NEERC15 E Easy Problemset (2015-2016 ACM-ICPC)(Codeforces GYM 100851)
- 北大暑假训练计划—POJ1207 The 3n + 1 problem(C语言)
- bnuoj 52317 As Easy As Possible(预处理+倍增法)
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
- 【As Easy As A+B - 专题训练-排序】
- As Easy As Possible
- 条款26:尽可能延后变量定义式的出现时间(Postpone variable definitions as long as possible)