题目链接
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
思路:这里PAT的计数与字符的顺序有关,比如APT这种顺序的不能算, 只有PAT这种顺序的才能算。
每次出现一个P,p++;在这些P的后面,若出现A,则a+=p(又多了p个PA组合);后面若出现一次T,则cnt+a,然后取模。
代码:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define drep(i,n,a) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a,n,sizeof(a))
#define lowbit(i) ((i)&(-i))
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f;
const double eps = 1e-6;
const int MOD=1e9+7;
const int N = 1e2+5;
int main() {
char ch;
int p=0,a=0,cnt=0;
while((ch=getchar())!='\n') {
if(ch=='P') {
p++;
} else if(ch=='A') {
a+=p;
} else {
cnt=(cnt+a)%MOD;
}
}
printf("%d",cnt);
return 0;
}