Note
-
以A为切入口,PAT个数等于A左侧P的个数*A右侧T的个数。
-
注意:取模mod的时候,不可以写成result+=(p*t)%100007;
正确写法:result = (result + countp * countt) % 1000000007;
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
int p=0,a=0,t=0,cnt=0;
cin>>str;
for(int i=0;i<str.length();i++){
if(str[i]=='T') t++;
}
for(int i=0;i<str.length();i++){
if(str[i]=='A') cnt=(cnt+(p*t))%1000000007;
if(str[i]=='P') p++;
if(str[i]=='T') t--;
}
cout<<cnt;
return 0;
}
本文介绍了一种计算字符串中特定模式PAT出现次数的高效算法。该算法利用了模式中各字符的位置特性,通过遍历字符串一次即可得出结果。关键在于利用左侧P的累积数量与右侧T的剩余数量来计算以每个A为中心的PAT组合数,并采取适当的方法进行取模运算避免溢出。
510

被折叠的 条评论
为什么被折叠?



