1040. 有几个PAT(25)
时间限制
120 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CAO, Peng
字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T);第二个PAT是第3位(P),第4位(A),第6位(T)。
现给定字符串,问一共可以形成多少个PAT?
输入格式:
输入只有一行,包含一个字符串,长度不超过105,只包含P、A、T三种字母。
输出格式:
在一行中输出给定字符串中包含多少个PAT。由于结果可能比较大,只输出对1000000007取余数的结果。
输入样例:APPAPT输出样例:
2
#include<stdio.h>
#include<string.h>
int main()
{
//freopen("E://input.txt", "r", stdin);
char s[100002];
while(scanf("%s", s) != EOF)
{
long long count = 0;
bool flag = true;//第一次遇见A
int count_p = 0;
int count_t = 0;
// 遇见第一个A,我需要找到A之前有多少P,A之后有多少T
for(int i = 0; i < strlen(s); i ++)
{
if(s[i] == 'A' && flag)
{
// count_p 保存A之前的P的个数
// count_t 保存A之后T的个数
count_p = 0;
count_t = 0;
// 计算第一个A之前有多少P
for(int j = 0; j < i; j ++)
if(s[j] == 'P')
count_p ++;
// 计算第一个A之后有多少T
for(int j = i+1; j < strlen(s); j ++)
if(s[j] == 'T')
count_t ++;
// 遇见第一个A,有多少个PAT
count = count + count_p *count_t;
count = count % 1000000007;
flag = false;
continue;
}
// 遇见第一个A之后,遇见P,P个个数+1
if(s[i] == 'P' && !flag)
count_p ++;
// 遇见第一个A之后,遇见T,T个个数-1
if(s[i] == 'T' && !flag)
count_t --;
// 遇见第一个A之后,每次遇见A就计算有多少个PAT
if(s[i] == 'A' && !flag)
{
count = count + count_p *count_t;
count = count % 1000000007;
}
}
printf("%lld\n", count);
}
return 0;
}