link: https://pintia.cn/problem-sets/994805342720868352/problems/994805373582557184
1093 Count PAT's (25 分)
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<stdio.h>
#include<stdlib.h>
#include<string.h>
int countT[100000];
int countP[100000];
int main(){
char* input=(char*)malloc(100000);
memset(input, '\0', sizeof(input));
scanf("%s", input);
int pNum=0,aNum=0;
int aCnt=0,tCnt=0,pCnt=0;
int sum=0;
for(int i=0; i < strlen(input); ++i){
switch(*(input+i)){
case 'P':
pNum++;
pCnt++;
aCnt=0;
break;
case 'A':
if(pNum != 0 ){
countP[aNum]=pCnt;
countT[aNum]=tCnt;
aNum++;
aCnt++;
tCnt=0;
}
break;
case 'T':
if(pNum != 0 && aNum != 0){
tCnt++;
}
break;
}
}
countA[pNum]=aCnt;
countT[aNum]=tCnt;
for(int i=aNum; i > 0; --i){
countT[i]=countT[i]+countT[i+1];
}
for(int i=1; i<= aNum; ++i){
sum += countP[i-1]*countT[i];
if(1000000007 <= sum){
sum %= 1000000007;
}
}
printf("%d\n",sum);
return 0;
}
总结:代码的缩进不美观是CSDN编辑器的问题
思路:求PAT的个数,求A前边有多少个P,A后边有多少个T
A字母在给定的Input字符串中是有顺序的,具体在数组中对应下标1,2,3 ....
例子:A1, A2, A3, A4, A5, A6, A7, ....
A1前边有多少个P字母,后边有多少个T字母
特殊处理:将第一个P字母出现之前的其他字母忽略。