克拉拉的博客

个人LEETCODE\PAT\OJ算法题解,知识见解

PAT.A1093. Count PAT's

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>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <iostream>
using namespace std;
int leftp[100010];
char a2[100010];
const int MOD = 1000000007;
int main(){
	scanf("%s", a2);
	int rightt = 0,sum=0;
	int l = strlen(a2);
	for (int i = 0; i < l; i++) {
		if (i > 0) leftp[i] = leftp[i - 1];
		if (a2[i] == 'P') leftp[i]++;
	 }
	for (int i = l - 1; i >= 0; i--) {
		if (a2[i] == 'T') rightt++;
		if (a2[i] == 'A')
			sum = (sum + leftp[i] * rightt) % MOD;
	}
	printf("%d", sum);
	return 0;
	
}

阅读更多
上一篇PAT.A1029. Median
下一篇PAT.A1101. Quick Sort
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭