Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine)

B - 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=A, if there is a bus station at ii-th crossroad, and si=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=A for all i≤t<j.

If for two crossroads ii and jj for all crossroads i,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 j if st=Bst=B for all i≤t<j.

For example, if ss=“AABBBAB”, a=4a=4 and b=3b=3 then Petya needs:
在这里插入图片描述

buy one bus ticket to get from 1 to 3,
buy one tram ticket to get from 3 to 6,
buy one bus ticket to get from 6 to 7.
Thus, in total he needs to spend 4+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 i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-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 t (1≤t≤104).

The first line of each test case consists of three integers a,b,pa,b,p (1≤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=A, if there is a bus station at i-th crossroad, and si=B, if there is a tram station at ii-th crossroad (2≤|s|≤105).

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

Output
For each test case print one number — the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from ii to n 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
题目大意: Petya有p卢布,他需要从镇子的一端乘坐巴士或地铁到镇子的另一端,A为巴士站,B为地铁站, Petya在和上车站点不同的站点下车,巴士站乘车需a卢布,地铁站乘车需b卢布,Petya需要走到可以连续坐车到家且不超额的站点上车,问他需要走至少多少步。
解题思路:
既然我们直接坐车回家,那么直接倒序找出从家到某个站点正好钱够或正好超额即可得知步数

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int a=0,b=0,p=0;
        int np=0,flag=0,j=0,step=0;		///np此时坐车已花费的钱数,flag 0 1判断最后一站足额还是超额
        char s[100001]={0},c={0};	///c用于存储最后一站,避免重站下车
        cin>>a>>b>>p;
        scanf("%s",s);
        int l=strlen(s);
        for(int i=l-2;i>=0;i--){
            if(s[i]=='A'&&c!=s[i]){
                np+=a;
            }
            if(s[i]=='B'&&c!=s[i]){
                np+=b;
            }
            c=s[i];
            if(np>p){				///超额
                j=i+1;
                flag=2;
                break;
            }
            if(np==p){			   ///足额
                j=i;
                flag=1;
                break;
            }

        }
        if(flag==1){				///因为是倒序所以最后一站仅仅遍历到了尾站,例如AAAAB,最后一站为A,但只遍历到第4个A,所需为第一个A的位置需再次遍历
            for(int i=j;i>=0;i--){
                if(s[i]==c){
                    step=i;
                }
                else{
                    break;
                }
            }
        }
        if(flag==2){
            step=j;
        }
        printf("%d\n",step+1);
    }
}

发布了12 篇原创文章 · 获赞 0 · 访问量 163

猜你喜欢

转载自blog.csdn.net/qq_45981086/article/details/104512904
今日推荐