此题关键是用存储空间去换取时间。否则老是超时。
在输入时就用一个vector《char》不断记录每一位时左边已经有的p,t。最后查找到a看对应位置左边的p个数,t个数,在求结果。
代码如下
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<char>::iterator ite;
int main(){
vector<char> vec;
vector<int> pleft, tleft;
char ch;
long long num = 0;
int p=0, t=0;//随手初始化!!!!!
while (ch = getchar()){
if (ch == '\n')
break;
if (ch == 'P'){
p++;
}
if (ch == 'T'){
t++;
}
pleft.push_back(p);
tleft.push_back(t);//pleft tleft vec 同时增长
vec.push_back(ch);
}
ite A = vec.begin();
while (A != vec.end()){
A = find(A, vec.end(), 'A');
if (A != vec.end()){
num += pleft[A - vec.begin()] * (t - tleft[A - vec.begin()]);
A++;
}
}
//这个方法还是要遍历好多次没办法用。不如价格变量,在输入时就记下来有多少pt、
//ite A = vec.begin();//少用+n来算。容易越界。
//while (A != vec.end()){
// A = find(A, vec.end(), 'A');
// p = count(vec.begin(), A, 'P');
// t = count(A , vec.end(), 'T');
// num += p*t;
// if (A != vec.end())
// A++;
//}
//老方法为先找到p再找a再找t,这样遍历次数比较多超时。新方法网上找到,先找到a左边p个数乘以右边t个数
// ite P = vec.begin(), A = vec.begin(), T = vec.begin();
// while (P!=vec.end()){//reduce to use。erase because it can change the iterator begin.
// P = find(P, vec.end(), 'P');
// if (P != vec.end())
// P++;
//
// A = P;
// while (A != vec.end()){
// A = find(A , vec.end(), 'A');
// num += count(A, vec.end(), 'T');
// if (A!=vec.end())
// A++;//不要随意自加。万一已经到了end再自加就会溢出。
// }
// }
cout << num%1000000007;
return 0;
}
//方法错误,不应该是从首尾同时删除。patpapatpatpatp这样就不行了
//#include<vector>
//#include<iterator>
//#include<iostream>
//#include<algorithm>
//
//using namespace std;
//
//int main(){
// vector<char> vec;
// int num = 0;
// char ch = 'P';
// while (ch=getchar()){//cin不读取回车,但是getchar读取。
// if (ch=='\n')//又把等于和赋值搞混了
// break;
// vec.push_back(ch);
// }
// while (!vec.empty()){
// if (flag % 2 = 1){
// vector<char>::iterator P = find(vec.begin(), vec.end(), 'P');
// vec.erase(vec.begin(), P - 1);
// if (P == vec.end())
// break;
// }
// if(flag%2=0){
// vector<char>::reverse_iterator T =
// find(vec.rbegin(), vec.rend(), 'T');
// if (T == vec.rend())
// break;
// vec.erase(T.base() - 1, vec.end());
// }
// if (P<T.base())
// num += count(P, T.base(), 'A');
//
//
// }
// cout << num % 1000000007;//取余数不是除数
//}