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.
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 the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
4 2 3 10 wwhw
2
5 2 4 13 hhwhh
4
5 2 4 1000 hhwhh
5
3 1 100 10 whw
0
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.
题意是给出正常看一张图片时间需要1,如果是水平方向的就要多一个旋转的时间b,然后切换图片需要时间a,从第一张图片可以切换到最后一张,从最后一张也可以切换到第一张。起始位置在第一张图片。问在给定的T时间内,能最多看多少张图片。
直接二分数量,因为起始位置给定,每次二分枚举向右最远到达的位置,向左的位置就能够求出,然后一共走完这些图片只有两种方式,切换的总值求一个最小值。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x3fffffffffffffff
const ll mod = 1e9 + 7;
const int maxn = 5e5 + 5;
ll n, a, b, t;
ll pri[maxn];
char x[maxn];
bool check(int mid)
{
ll i, j, k;
ll sum1, sum2;
for (i = 1; i <= mid; i++)
{
sum1 = pri[i] + (pri[n] - pri[n - (mid - i)]);
sum2 = min((i - 1)*a * 2 + (mid - i)*a, (i - 1)*a + (mid - i)*a * 2);
if (sum1 + sum2 <= t)
{
return true;
}
}
return false;
}
void solve()
{
int i, j, k;
scanf("%I64d%I64d%I64d%I64d", &n, &a, &b, &t);
scanf("%s", x + 1);
for (i = 1; i <= n; i++)
{
pri[i] = pri[i - 1] + 1 + (x[i] == 'w' ? b : 0);
}
int le = 0, ri = n, mid;
while (le < ri)
{
mid = (le + ri + 1) / 2;
if (check(mid))
{
le = mid;
}
else
{
ri = mid - 1;
}
}
printf("%d", le);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
solve();
return 0;
}