题目链接
题意: Petya有p卢布,他需要坐车回家,A为车站需a卢布,B为地铁站需b卢布,他从车站(地铁站)始发处上车,到地铁站(车站)始发处下车,然后在该位置继续坐车,(Please note that the type of the stop at the last crossroad (i.e. the character snsn) does not affect the final expense.说明最后一站不用考虑花费情况),Petya可走一段距离,直到他能从该位置一直乘车到家,问至少多少步。
倒着找车站
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
int n,a,i,b,q,c;
string s;
cin>>n;
while(n--)
{
cin>>a>>b>>q>>s;
for(i=s.length()-2,c=0; i>=0; i--)
{//i=s.length()-2因为i的下标从0开始,所以末尾的下标为s.length()-1,又因为最后一站无影响不考虑,所以是s.length()-2,即从倒数第二站开始。
if(c!=s[i])//c用来记录'A''B'保证连续的A或B当一种情况处理。
{
c=s[i];//记录ASCII码的值。
q-=(c=='A'? a: b);//用"?:"简化步骤。
if (q<0) break;//终止条件。
}
}
cout<<i+2<<endl;//i+1表示实际位置,再加一为到下一站站点的位置。
}
return 0;
}