After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.

The crossroads are represented as a string ss of length nn , where si=Asi=A , if there is a bus station at ii -th crossroad, and si=Bsi=B , if there is a tram station at ii -th crossroad. Currently Petya is at the first crossroad (which corresponds to s1s1 ) and his goal is to get to the last crossroad (which corresponds to snsn ).

If for two crossroads ii and jj for all crossroads i,i+1,…,j−1i,i+1,…,j−1 there is a bus station, one can pay aa roubles for the bus ticket, and go from ii -th crossroad to the jj -th crossroad by the bus (it is not necessary to have a bus station at the jj -th crossroad). Formally, paying aa roubles Petya can go from ii to jj if st=Ast=A for all i≤t<ji≤t<j .

If for two crossroads ii and jj for all crossroads i,i+1,…,j−1i,i+1,…,j−1 there is a tram station, one can pay bb roubles for the tram ticket, and go from ii -th crossroad to the jj -th crossroad by the tram (it is not necessary to have a tram station at the jj -th crossroad). Formally, paying bb roubles Petya can go from ii to jj if st=Bst=B for all i≤t<ji≤t<j .

For example, if ss ="AABBBAB", a=4a=4 and b=3b=3 then Petya needs:

  • buy one bus ticket to get from 11 to 33 ,
  • buy one tram ticket to get from 33 to 66 ,
  • buy one bus ticket to get from 66 to 77 .

Thus, in total he needs to spend 4+3+4=114+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character snsn ) does not affect the final expense.

Now Petya is at the first crossroad, and he wants to get to the nn -th crossroad. After the party he has left with pp roubles. He's decided to go to some station on foot, and then go to home using only public transport.

Help him to choose the closest crossroad ii to go on foot the first, so he has enough money to get from the ii -th crossroad to the nn -th, using only tram and bus tickets.

Input

Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104 ).

The first line of each test case consists of three integers a,b,pa,b,p (1≤a,b,p≤1051≤a,b,p≤105 ) — the cost of bus ticket, the cost of tram ticket and the amount of money Petya has.

The second line of each test case consists of one string ss , where si=Asi=A , if there is a bus station at ii -th crossroad, and si=Bsi=B , if there is a tram station at ii -th crossroad (2≤|s|≤1052≤|s|≤105 ).

It is guaranteed, that the sum of the length of strings ss by all test cases in one test doesn't exceed 105105 .

Output

For each test case print one number — the minimal index ii of a crossroad Petya should go on foot. The rest of the path (i.e. from ii to nn he should use public transport).

Example
Input

 
5
2 2 1
BB
1 1 1
AB
3 2 8
AABBBBAABB
5 3 4
BBBBB
2 1 1
ABABAB
Output

 
2
1
3
1
6
大意就是这个人有p元,公交车在乘车区间a元,有轨电车b元,要到达n的话钱不一定够,问先步行再坐交通工具的话最少需要走几站。从后往前考虑比较方便。根据题意,其实s[n]是什么不用管,因为要到达的就是s[n],所以分别求出s[n]到s[i](0<=i<=n-1,这里是字符串下标)所需要花的钱。容易看出花费构成的序列是单调的因此直接从里面找第一个小于等于p的数对应的位置输出即可,没有的话直接输出最后一个位置。理论上这里可以直接二分lower_bound,不知道为什么这么写会挂在第56个点...最后O(n)竟然也能过...还请大佬指教。
#include <bits/stdc++.h>
using namespace std;
long long a,b,p;
char s[];
unsigned long long n[];
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%lld%lld%lld",&a,&b,&p);
long long i,pre=;
scanf("%s",s);
for(i=strlen(s)-;i>=;i--)//不用管最后一个是啥
{
long long temp=;
if(s[i]=='A')temp=a;
else if(s[i]=='B')temp=b;
if(i==strlen(s)-)
{
pre=temp;
n[i]=temp;
continue;
}
if(s[i]==s[i+])n[i]=n[i+];
else
{
pre=n[i+];
n[i]=pre+temp;
}
}
// long long pos=lower_bound(n,n+strlen(s)-1,p,greater<int>())-n;//找到递减序列里第一个小于等于p的数 //为啥二分过不了???????
// if(pos==strlen(s))
// {
// cout<<strlen(s)<<endl;
// continue;
// }
// cout<<pos+1<<endl;
int pos=;
bool flag=;
for(i=strlen(s)-;i>=;i--)
{
if(i==)
{
if(n[]<=p)
{
pos=;
flag=;
}
}
if(n[i]<=p&&n[i-]>p)
{
pos=i+;
flag=;
break;
}
}
if(!flag)pos=strlen(s);
cout<<pos<<endl;
}
return ;
}

Codeforces 1315B Homecoming (二分)的更多相关文章

  1. codeforces 1165F1/F2 二分好题

    Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...

  2. codeforces 732D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...

  3. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  4. CodeForces 163B Lemmings 二分

    Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...

  5. CodeForces - 589A(二分+贪心)

    题目链接:http://codeforces.com/problemset/problem/589/F 题目大意:一位美食家进入宴会厅,厨师为客人提供了n道菜.美食家知道时间表:每个菜肴都将供应. 对 ...

  6. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  7. CodeForces - 1059D(二分+误差)

    链接:CodeForces - 1059D 题意:给出笛卡尔坐标系上 n 个点,求与 x 轴相切且覆盖了所有给出点的圆的最小半径. 题解:二分半径即可.判断:假设当前二分到的半径是 R ,因为要和 x ...

  8. Letters CodeForces - 978C (二分)

    Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...

  9. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

随机推荐

  1. ansible-七种武器

    1. ansible命令 2. ansible-doc是ansible模块说明文档,针对每个模块都有详细用法说明以及应用案例介绍 3. ansible-console是ansible为用户提供的一款交 ...

  2. 微信小程序中如何上传图片

    本篇文章给大家带来的内容是关于微信小程序中如何上传图片(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 一.wxml文件 <text>上传图片</text ...

  3. 【C语言】【欢哥TV】冒泡法排序视频演示

    fromB站 点击进入:Go

  4. Hbase javaAPI(工具类)表的增删改查

    建立连接: package Init; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; i ...

  5. phpstorm更换主题

    打开PhpStorm,File -- Settings -- Editor -- Color Scheme --General选择你喜欢的风格进行更改,选择完成后单击Apply

  6. 配置maven时,报JAVA_HOME错误

    其实原因很简单,是之前java的jdk配置不合规范! 一.我是win10系统. 二.我的java jdk放在D盘,环境变量里,JAVA_HOME是D:\Program Files\Java\jdk1. ...

  7. liunx下安装Docker

    1.安装并启动docker 1.检查内核版本,必须是3.10及以上uname -r2.安装docker yum install docker命令安装(需要联网) [root@localhost ~]# ...

  8. redis 有序集合(set),无需集合(zset)

    1.set(无序集合)无序集合每个元素都是string元素的唯一性,不能重复没有修改操作 1.增加 sadd key value1 value2 value3... 2.获取 smembers key ...

  9. Python入门8 —— 逻辑运算符补充

    一:优先级:not > and > or 1.not与紧跟其后的那个条件是不可分割的 2.如果条件语句全部由纯and.或纯or链接,按照从左到右的顺序依次计算即可 print(True a ...

  10. OpenGL 编程指南 (5.1)

    1.OpenGL支持同时使用多个纹理单元,使用GL_TEXTUREi进行标识,使用前需要先激活对应的纹理单元,默认GL_TEXTURE0是激活绑定的. void glActiveTexture(GLe ...