Loading

Codeforces 1315B Homecoming (二分)

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,,j1i,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 it<ji≤t<j .

If for two crossroads ii and jj for all crossroads i,i+1,,j1i,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 it<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 (1t1041≤t≤104 ).

The first line of each test case consists of three integers a,b,pa,b,p (1a,b,p1051≤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[1000005];
unsigned long long n[1000005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%lld%lld%lld",&a,&b,&p);
        long long i,pre=0;
        scanf("%s",s);
        for(i=strlen(s)-2;i>=0;i--)//不用管最后一个是啥 
        {
            long long temp=0;
            if(s[i]=='A')temp=a;
            else if(s[i]=='B')temp=b;
            if(i==strlen(s)-2)
            {
                pre=temp;
                n[i]=temp;
                continue;
            }
            if(s[i]==s[i+1])n[i]=n[i+1];
            else
            {
                pre=n[i+1];
                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=0;    
        bool flag=0;
        for(i=strlen(s)-2;i>=0;i--)
        {
            if(i==0)
            {
                if(n[0]<=p)
                {
                    pos=1;
                    flag=1;
                }
            }
            if(n[i]<=p&&n[i-1]>p)
            {
                pos=i+1;
                flag=1;
                break;
            }
        }
        if(!flag)pos=strlen(s);
        cout<<pos<<endl;
    }
    return 0;
 } 

 


posted @ 2020-02-25 20:27  脂环  阅读(399)  评论(0编辑  收藏  举报