标签:str alt include code using div style pre clu
分析:
PPPATTTTPAT
第一个A的左边有3个P,右边有5个T,此时PAT共有15个。
第二个A的左边有4个P,右边有1个T,此时PAT共有4个。
这个字符串包含的PAT总共有19个。
总结,只要知道每个A的左边共有m个P,右边共有n个T,此时PAT的个数位即为m*n;
#include<iostream> using namespace std; typedef long long LL; char a[100010] = {0}; int main() { LL i = 0,cnt_P = 0,cnt_T = 0,ans = 0; while(scanf("%c",&a[i])!=EOF) { if(a[i] == ‘T‘) cnt_T++;//统计T的个数 ++i; } for(LL j = 0; j < i; ++j) { if(a[j] == ‘A‘) ans = (ans+ cnt_P*cnt_T)%1000000007;//注意:ans也要参与运算后再取模 else if(a[j] == ‘P‘) cnt_P++;//统计当前位左边的P的个数 else if(a[j] == ‘T‘) cnt_T--;//统计当前位右边的T的个数 else ; } cout<<ans; return 0; }
标签:str alt include code using div style pre clu
原文地址:https://www.cnblogs.com/keep23456/p/12330721.html