首页 > 学院 > 开发设计 > 正文

1093. Count PAT's (25)

2019-11-14 08:55:01
字体:
来源:转载
供稿:网友

The string APPAPT contains two PAT’s as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.

Now given any string, you are supposed to tell the number of PAT’s contained in the string.

Input Specification:

Each input file contains one test case. For each case, there is only one line giving a string of no more than 105 characters containing only P, A, or T.

Output Specification:

For each test case, PRint in one line the number of PAT’s contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

Sample Input: APPAPT Sample Output: 2

#include<cstdio>#include<cstring>const int maxn=100010;const int MOD=1000000007;char a[maxn];int leftNumP[maxn]={0};int main(){ scanf("%s",a); int len=strlen(a); for(int i=0;i<len;i++){ if(i>0){ leftNumP[i]=leftNumP[i-1]; } if(a[i]=='P') leftNumP[i]=leftNumP[i-1]+1; } int cntt=0,ans=0; for(int i=len-1;i>=0;i--){ if(a[i]=='T') cntt++; if(a[i]=='A'){ ans=(ans+leftNumP[i]*cntt)%MOD; } } printf("%d/n",ans);// long long cnt=0;// for(int i=0;i<len;i++){// if(a[i]=='P'){// for(int j=i+1;j<len;j++){// if(a[j]=='A'){// for(int k=j+1;k<len;k++){// if(a[k]=='T'){// cnt++;// }// }// }// }// }// }// long long cnt=0,cntp=0;// for(int i=0;i<len;i++){// if(a[i]=='P') cntp++;// if(a[i]=='A'){// long long cntt=0;// for(int j=i+1;j<len;j++){// if(a[j]=='T') cntt++;// }// cnt=(cnt+cntp*cntt)%MOD;// }// }// printf("%lld/n",cnt); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表