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. Struts2学习-struts执行过程简述

    1.web.xml <web-app> <filter> <filter-name>struts2</filter-name> <filter-c ...

  2. 【C语言】输出半径1到10的圆的面积,当面积值超过100时,停止执行本程序

    #include<stdio.h> #define PI 3.142 int main() { int r; float area; ; r <= ; r++) { area = P ...

  3. VSCode的Vue插件Vetur设置

    使用VSCode编写vue项目时安装了Vetur插件,但是每次alt+shift+f格式化代码的时候就有点让人头疼, 缩进自动变成了2个空格(习惯了用4个空格缩进,不同层级的代码看着明显一点),js代 ...

  4. Linux C/C++ 字符串逆序

    /*字符串逆序*/ #include <stdio.h> #include <string.h> void nixu(char *str) { ; char tmp; for( ...

  5. Python读取Excel,日期列读出来是数字的处理

    Python读取Excel,里面如果是日期,直接读出来是float类型,无法直接使用. 通过判断读取表格的数据类型ctype,进一步处理. 返回的单元格内容的类型有5种: ctype: 0 empty ...

  6. AE创建组件失败,项目中已存在对esri.arcgis.***的引用

    AE创建组件失败,项目中已存在对esri.arcgis.***的引用 解决办法:在解决方案资源管理器的引用中把错误提示中的引用删掉,再创建组件就没问题了.

  7. 微信小程序中的左右联动

    微信小程序端的左右联动-滚动效果插件: 效果图如下:                                                                          ...

  8. 【Python】字符串的格式化

    一一对应  符号要用英文半角形式

  9. Java EE开发课外事务管理平台

    Java EE开发课外事务管理平台 演示地址:https://ganquanzhong.top/edu 说明文档 一.系统需求 目前课外兴趣培训学校众多,完善,但是针对课外兴趣培训学校教务和人事管理信 ...

  10. k线中转器

    自动同步服务器k线,将交易日k线存入共享内存,交易平台直接去共享内存取想要的数据. 默认提供期货1.3.5分钟.日线数据.如果想要自定义,可以通过copydata向它发送请求,可以提供任何周期,任何偏 ...