time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.

For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.

Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.

Help Vasya find the maximum number of photos he is able to watch during T seconds.

Input

The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.

Second line of the input contains a string of length n containing symbols 'w' and 'h'.

If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.

If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.

Output

Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.

Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note

In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.

Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.

-------------------------------------------------------

仔细读题啊!

Solution:

看了题解过的,果然萎得不行。

我能想到的:

看的图片必然连续,因为每次只能选择看右边或左边还没看的那张图。

最后看的图的分布:

#(n-l)#(n-l+1)#(n-l+2)#...#(n-1)#(n)#(1)#(2)#(3)...#(r)

|<--------------------------------------------             

|_____________________________------------------->

               --------------------- >|

<-----------------------------------------————————|

(#表示图片,括号内是其ID,虚线表示看图,实线表示跳过)

但是我TM就是没看出来要达到最有解,翻图的路线只有两种情况:

对于给定的(l, r)看图的时间(包括调整方向与看)是固定,所以总时间取决于翻图的次数。

---------------------------------------------------------------------------------------------------------------------

所以最优解只有四种情况:

1.一直向右翻

2.一直向左翻

3.先向左翻,再向右翻

4.先向右翻再向左翻

后两种情况可以用双指针(two-pointers)做。

------------------------------------------------------------------

Implementation:

two-pointers写得磕磕绊绊,coding弱得不行,sigh.

(Ignore the comment like a compiler :D)

#include <bits/stdc++.h>
using namespace std; typedef long long LL; const int N(5e5+); char o[N];
int n, a, b, T; int cost(int x){
return +(o[x]=='w')*b+(bool)x*a;
} int main(){
for(;cin>>n>>a>>b>>T>>o;){
int ans=;
for(;cost()<=T;){
int t=T-cost(), i, j;
//two-pointers
for(i=; i<n; i++){
if(cost(i)>t) break;
t-=cost(i);
}
ans=max(ans, i);
if(ans==n) break; // L.I.:
// i-1: current position to the righ;
for(t-=(i-)*a, j=; i>=; t+=cost(i-)+a, i--){
// L.I.:
// j:offset to the left
// n-j-1:current left position
for(;j<n-i && cost(n-j-)<=t; j++){
t-=cost(n-j-);
}
ans=max(ans, i+j);
} if(ans==n) break; t=T-cost();
for(i=; i<n; i++){
if(cost(n-i)>t) break;
t-=cost(n-i);
} ans=max(ans, i);
if(ans==n) break; // n-i+1: last position
for(t-=(i-)*a, j=; i>=; t+=cost(n-i+)+a, i--){//one step back
for( ; j<n-i && cost(j+)<=t; j++){
t-=cost(j+);
}
ans=max(ans, i+j);
}
break;
} cout<<ans<<'\n';
}
return ;
}

最新文章

  1. java泛型详解
  2. Mysql 之旅开始啦
  3. Spring MVC配置静态资源的正常访问
  4. jqeury.base
  5. air开发中的requestedDisplayResolution 扫盲
  6. maven缺少依赖包,强制更新命令
  7. Newtonsoft.Json.dll使用
  8. .NET小项目之MyKtv(歌曲播放功能实现)
  9. gettid()和pthread_self()的区别
  10. Knockoutjs官网翻译系列(四) computed中依赖追踪是如何工作的
  11. 实现QQslidingMenu侧滑效果学习笔记
  12. 怎么使用dreamweaver制作网页教程 dw建站设计网页
  13. Rx学习
  14. Vue2.0音乐播放器
  15. BigDecimal与Long之间的转换
  16. UVa 725 简单枚举+整数转换为字符串
  17. Spark中SQL列和并为一行
  18. IIC详解
  19. threading.local学习
  20. hbase hbck命令

热门文章

  1. Hibernate入门注解笔记
  2. window7 右键菜单显示-》在此处打开命令窗口
  3. [tools]神器notepad++
  4. shell案例
  5. 七种css方式让一个容器水平垂直居中
  6. 详解C# 迭代器[转]
  7. 获取技能的成功经验和关于C语言学习的调查 2015528
  8. 备份U盘分区表,未雨绸缪
  9. 嵌入式linux驱动开发之给linux系统添加温度传感器模块
  10. RockWare RockWorks的Ollydbg调试过程及注册机(破解)思路