1040 有几个PAT

字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T);第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位(T)。

现给定字符串,问一共可以形成多少个 PAT

输入格式:

输入只有一行,包含一个字符串,长度不超过105​​,只包含 PAT 三种字母。

输出格式:

在一行中输出给定字符串中包含多少个 PAT。由于结果可能比较大,只输出对 1000000007 取余数的结果。

输入样例:

APPAPT

输出样例:

2
 
思路:先从头到尾遍历一遍字符串,在每个A处标记为这个A之前的P的数量,然后再从后到前遍历,每找到一个T,统计一次数量,直到遇到A进行计算,记得边算边mod......
 
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #define mod 1000000007
 6 #define max 100001
 7 int main()
 8 {
 9     char str[max];
10     gets(str);
11     int len=strlen(str);
12     int left_P[len],count=0;
13     memset(left_P,0,sizeof(int));
14     for(int i=0;i<len;i++)
15     {
16         if(str[i]=='P')
17             count++;
18         else if(str[i]=='A')
19             left_P[i]=count;
20     }
21     count=0;
22     int sum=0;
23     for(int i=len-1;i>=0;i--)
24     {
25         if(str[i]=='T')
26             count++;
27         else if(str[i]=='A')
28         {
29             sum+=left_P[i]*count;
30             sum%=mod;
31         }
32     }
33     printf("%d
",sum);
34     return 0;
35 }
大佬见笑,,
原文地址:https://www.cnblogs.com/xwl3109377858/p/10475137.html