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. Python调用百度地图API实现批量经纬度转换为实际省市地点(api调用,json解析,excel读取与写入)

    1.获取秘钥 调用百度地图API实现得申请百度账号或者登陆百度账号,然后申请自己的ak秘钥.链接如下:http://lbsyun.baidu.com/apiconsole/key?applicatio ...

  2. wx: wx.showModal 回调函数中调用自定义方法

    一.在回调函数中调用自定义方法: 回调函数中不能直接使用this,需要在外面定义 var that = this 然后 that.自定义的方法.如下: //删除 onDelete: function ...

  3. 3.Docker 操作镜像

    获取镜像 之前提到过,Docker Hub 上有大量的高质量的镜像可以用,这里我们就说一下怎么获取这些镜像. 从 Docker 镜像仓库获取镜像的命令是 docker pull.其命令格式为: doc ...

  4. MyBatis的手动映射与模糊查询

    一.手动映射 当实体类属性与数据库字段名不同时,无法自动映射,导致查询出空值,这时候可以使用手动映射 在select节点添加resultMap属性与resultMap节点建立关系

  5. 如何架构一个 React 项目?

    编程有点像搞园艺.比起竭力去对付BUG(虫子),我们更愿意把一切弄得整洁有序,以免最后落得个身在荒野丛林中.低劣的架构会拖我们的后腿,也会使得BUG更容易钻进系统里去. 想要对你的项目进行架构,方法有 ...

  6. VMware vSphere Client

    复制虚拟机 在虚拟机关机状态下,选中一个虚拟机,文件 - 导出 - 导出OVF模板,导出成功后,再文件 - 部署OVF模板(修改IP.MAC.主机名称)

  7. 理解 Oracle 多租户体系中(12c,18c,19c)创建角色作用域范围

    本篇探讨以下几个问题:你可提前猜测下面6个场景语句中,哪几个可以成功创建角色? 1. 在CDB级别中创建公共角色,不带 container 子句的效果: 2. 在CDB级别中创建公共角色,带 cont ...

  8. python调用HEG工具批量处理MODIS数据

    下面的代码主要用于使用python语言调用NASA官方的MODIS处理工具HEG进行投影坐标转换与重采样批量处理 主要参考 HEG的用户手册:https://newsroom.gsfc.nasa.go ...

  9. 四、CentOS 7安装Oracle JDK

    CentOS 7安装Oracle JDK,查看Linux是否自带的JDK,如有openJDK,则卸载  CentOS7.1 JDK安装 1.卸载自带OPENJDK    用 java -version ...

  10. 题解 洛谷 P4145 【上帝造题的七分钟2 / 花神游历各国】

    题目 上帝造题的七分钟2 / 花神游历各国 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. ...