2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
题目链接
https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A
problem description
As we know, the NTU Final PK contest usually tends to be pretty hard. Many teams got frustrated when participating 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
题意:给了一个只含有'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], 查询时,先找到v=p[r] 然后找左边有效字符个数,最后除以4 就是结果;
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
char s[];
int a[],p[];
int anc[][];
int mp[]; int main()
{
int m;
while(scanf("%s",s+)!=EOF)
{
int len=strlen(s+);
for(int i=;i<=len;i++)
{
if(s[i]=='e') a[i]=;
else if(s[i]=='a') a[i]=;
else if(s[i]=='s') a[i]=;
else a[i]=;
}
memset(mp,,sizeof(mp));
for(int i=;i<=len;i++)
{
int pre=(a[i]-+)%;
anc[i][]=mp[pre];
mp[a[i]]=i;
p[i]=mp[];
}
for(int i=;i<=;i++)
{
for(int j=;j<=len;j++)
{
anc[j][i]=anc[anc[j][i-]][i-];
}
}
scanf("%d",&m);
while(m--)
{
int l,r;
int sum=;
scanf("%d%d",&l,&r);
int v=p[r];
for(int i=;i>=;i--)
{
if(anc[v][i]>=l){
sum+=(<<i);
v=anc[v][i];
}
}
printf("%d\n",sum/);
}
}
return ;
}
2016弱校联盟十一专场10.5---As Easy As Possible(倍增)的更多相关文章
- 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...
- (2016弱校联盟十一专场10.3) D Parentheses
题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...
- (2016弱校联盟十一专场10.3) B.Help the Princess!
题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...
- (2016弱校联盟十一专场10.3) A.Best Matched Pair
题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...
- 2016弱校联盟十一专场10.3---We don't wanna work!(STL--set的使用)
题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8504#problem/C 代码如下: #include <iostream> # ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search
题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...
- (2016弱校联盟十一专场10.2) E.Coins
题目链接 很久之前写的了,好像是对拍打表过的,推一下就行了. #include <bits/stdc++.h> using namespace std; typedef long long ...
- (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci
题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream&g ...
随机推荐
- Excel快速改变行列的次序
改变行列次序是在Excel中常常需要进行的操作,多数用户的方法是先剪切要调整的行或列,然后选定目标位置,单击菜单“插入”→“剪切单元格”. 事实上,使用键盘来配合的话,改变行列的次序可以更快捷.比 ...
- Atitit.会员卡(包括银行卡)api的设计
Atitit.会员卡(包括银行卡)api的设计 1. 银行卡的本质是一种商业机构会员卡1 2. 会员卡号结构组成1 2.1. ●前六位是:发行者标识代码 Issuer Identification N ...
- salesforce 零基础学习(三十)工具篇:Debug Log小工具
开发中查看log日志是必不可少的,salesforce自带的效果显示效果不佳,大概显示效果如下所示: chrome商城提供了apex debug log良好的插件,使debug log信息更好显示.假 ...
- iOS-旧项目中手动内存管理(MRC)转ARC
在ARC之前,iOS内存管理无论对资深级还是菜鸟级开发者来说都是一件很头疼的事.我参 加过几个使用手动内存管理的项目,印象最深刻的是一个地图类应用,由于应用本身就非常耗内存,当时为了解决内存泄露问题, ...
- css基础总结一
最近在弄一个简单管理系统的前端,所以打算将做项目的一些个人感想以及总结简单罗列下,当然,主要针对前端的基础部分以及一些常用的前端个人简单技巧总结.主要分为js部分和css部分,下面是css的基础部分总 ...
- python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...
- 让我们一起写出更有效的CSharp代码吧,少年们!
周末空闲,选读了一下一本很不错的C#语言使用的书,特此记载下便于对项目代码进行重构和优化时查看. Standing On Shoulders of Giants,附上思维导图,其中标记的颜色越深表示在 ...
- ASP.NET MVC中简单使用Autofac
项目中引入Autofac的目的是为了实现控制反转,即IoC,Inversion of Control.控制反转可以有效的降低类之间的相互依赖关系,增加架构的弹性,降低软件复杂度. 示例代码: IPro ...
- Android属性动画之ValueAnimation
ValueAnimation是ObjectAnimation类的父类,经过前几天的介绍,相信大家对ObjectAnimation有了 一定的认识,今天就为大家最后介绍一下ValueAnimation, ...
- PL/SQL异常处理
As we all known,程序的错误一般分为两类:编译错误和运行时错误.其中运行时错误被称为异常.PL/SQL语句块中处理异常的部分即为异常处理部分.在异常处理部分,可以指定当特定异常发生时所采 ...